package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.AttendJdbc;
import com.wuseguang.springcloud.dao.mapper.AttendMapper;
import com.wuseguang.springcloud.dao.repository.AttendRepository;
import com.wuseguang.springcloud.entity.Grade;
import com.wuseguang.springcloud.service.AttendService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.DateUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.*;

/**
 * <p>
 * 班牌人脸或刷卡考勤 服务实现类
 * </p>
 *
 * @author
 * @since 2020-03-02
 */
@Service
public class AttendServiceImpl implements AttendService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AttendRepository attendRepository;
    @Autowired
    private AttendJdbc attendJdbc;

    @Autowired
    private AttendMapper mapper;

    private static String weidaka = "未打卡";

    private static String queqin = "缺勤";


    public String getAttendType(String type) {
        //创建map 并且赋值
        Map<String, String> map = new HashMap<String, String>() {
            {//考勤类型: 0请假 1进班正常 2进班迟到 3进班缺卡  4离班正常 5离班早退  6离班缺卡 7休息 8缺勤
                //put("0", "病假");
                put("0", "请假");
                put("1", "进班正常");
                put("2", "进班迟到");
                put("3", "进班缺卡");
                put("4", "离班正常");
                put("5", "离班早退");
                put("6", "离班缺卡");
                put("7", "休息");
                put("8", "缺勤");
            }
        };

        return map.get(type);
    }

    //考勤 2.0
    @Override
    public List<Map<String, Object>> queryStudentAttend2(String attend_groupid, String school_id, String dateTime, String classId) {
        String gettime = DateUtil.time();
        if (StringUtils.isEmpty(dateTime)) {
            dateTime = DateUtil.time();
        }
        //按班级查询学生
        String sql = " SELECT t.id,t.student_name,t.head_portrait FROM student t,attend_group_user g WHERE t.id=g.userid AND g.attend_group_id=? ";
        String classIdString = "";
        if (!StringUtils.isEmpty(classId)) {//按班级筛选
            classIdString = " AND t.class_id in (" + classId + ") ";
        }
        sql += classIdString;
        sql += " group by t.id";
        List<Map<String, Object>> studentList = attendJdbc.queryListMap(sql, attend_groupid);

        if (studentList == null || studentList.size() <= 0) {//考勤组没有学生
            return null;
        }

        if (DateUtil.getDay(gettime, dateTime) == 0) {//查询当天数据
            int weeksInt = DateUtil.getWeeksInt(gettime);
            sql = "SELECT IF(shift_id!=0,shift_id,(SELECT shift_id FROM attend_group_shift WHERE attend_group_id=attend_group.id AND sort=? ) ) ints FROM attend_group WHERE school_id=? AND id=?";
            Integer shift_id = attendJdbc.queryInteger(sql, weeksInt, school_id, attend_groupid);
            if (shift_id == 0 || shift_id == -1) {//当天时间为休息
                for (int i = 0; i < studentList.size(); i++) {
                    Map<String, Object> sMap = studentList.get(i);
                    List<Map<String, Object>> maps = new ArrayList<>();
                    Map<String, Object> map = new HashMap<>();
                    map.put("state", "休息");
                    maps.add(map);
                    sMap.put("state", 0);
                    sMap.put("list", maps);
                }
            } else {

                sql = "SELECT  GROUP_CONCAT( t.attendtime  ORDER BY t.attendtime  ) attend,t.userid FROM class_attendlist t WHERE   t.attend_group_id=?   AND TO_DAYS(t.opertime)=TO_DAYS(?)  GROUP BY t.userid";
                Map<String, String> attendMap = null;
                try {
                    attendMap = attendJdbc.queryAttendMap(sql, attend_groupid, dateTime);
                } catch (Exception e) {
                    logger.info("考勤2.0 没有考勤数据。。");
                }
                sql = "SELECT  f.type,f.starttime_one ,f.endtime_one ,f.starttime_two ,f.endtime_two   FROM   attend_shift f WHERE  f.id=?";
                Map<String, Object> shiftMap = null;
                try {
                    shiftMap = attendJdbc.queryMap(sql, shift_id);
                } catch (Exception e) {
                    logger.info("queryAttendUserStatusAll 2.0-->erorr={}", e);
                }


                Integer type = (int) shiftMap.get("type");//考勤类型:0 1天1次、1 1天2次
                String starttime_one = shiftMap.get("starttime_one") + "";
                String endtime_one = shiftMap.get("endtime_one") + "";
                String starttime_two = shiftMap.get("starttime_two") + "";
                String endtime_two = shiftMap.get("endtime_two") + "";

                String starttimeOne = DateUtil.AddTime(gettime, starttime_one);
                String endtimeOne = DateUtil.AddTime(gettime, endtime_one);
                String starttimeTwo = DateUtil.AddTime(gettime, starttime_two);
                String endtimeTwo = DateUtil.AddTime(gettime, endtime_two);

                for (int i = 0; i < studentList.size(); i++) {
                    int qk = 0;//当天 缺卡次数

                    int a = 0;//"进班缺卡"
                    int b = 0;//"进班迟到"
                    int c = 0;//"离班缺卡"
                    int d = 0;//"离班早退"
                    int e = 0;//"进班正常"
                    int f = 0;//"离班正常"


                    int A = 0;//"进班缺卡"
                    int B = 0;//"进班迟到"
                    int D = 0;//"离班早退"
                    int E = 0;//"进班正常
                    int F = 0;//"离班正常


                    Map<String, Object> sMap = studentList.get(i);
                    String id = sMap.get("id") + "";
                    List<Map<String, Object>> maps = new ArrayList<>();
                    if (type == 0) {
                        String attendString = "未打卡";
                        String time = "";
                        if (!StringUtils.isEmpty(endtimeOne) && !StringUtils.isEmpty(starttimeTwo)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                            long l = DateUtil.getcompTime(starttimeTwo, endtimeOne);
                            time = DateUtil.addtimeMinutes(endtimeOne, (int) l / 2);
                        } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                            long l = DateUtil.getcompTime(endtimeTwo, starttimeOne);
                            time = DateUtil.addtimeMinutes(starttimeOne, (int) l / 2);
                        }

                        if (attendMap == null || attendMap.get(id) == null) {//今天没有考勤
                            if (DateUtil.valueCompTime(DateUtil.time(), time) > 0) {
                                attendString = "进班缺卡";
                            }
                            Map<String, Object> map = new HashMap<>();
                            map.put("dktime", starttime_one);
                            map.put("name", "进班");
                            map.put("attend", attendString);
                            maps.add(map);
                            Map<String, Object> map2 = new HashMap<>();
                            map2.put("dktime", endtime_two);
                            map2.put("name", "离班");
                            maps.add(map2);
                            sMap.put("state", 1);
                            sMap.put("list", maps);
                        } else {
                            String jTime = "";
                            String lTime = "";
                            int index = 0;
                            String[] split = attendMap.get(id).split(",");
                            for (int j = 0; j < split.length; j++) {
                                String attendtime = split[j];
                                if (DateUtil.valueCompTime(time, attendtime) >= 0) {//第一段考勤是否有考勤
                                    if (DateUtil.valueCompTime(starttimeOne, attendtime) >= 0) {
                                        e = 1;//"进班正常"
                                        jTime = attendtime;
                                    }
                                    if (DateUtil.valueCompTime(time, attendtime) > 0) {//"进班迟到"
                                        if (e == 0) {
                                            b = 1;
                                            jTime = attendtime;
                                            index = 1;
                                        }
                                    }
                                }
                                if (DateUtil.valueCompTime(time, attendtime) <= 0 && DateUtil.valueCompTime(endtimeTwo, attendtime) >= 0) {//"离班早退"
                                    if (f == 0) {
                                        d = 1;
                                        lTime = attendtime;
                                        index = 1;
                                    }
                                }
                                if (DateUtil.valueCompTime(endtimeTwo, attendtime) < 0) {//"离班正常"
                                    if (d == 0) {
                                        f = 1;
                                        lTime = attendtime;
                                    }
                                }
                            }
                            if (e == 0 && b == 0) {
                                a=1;
                                qk = 1;//"缺卡"
                                index = 1;
                            }
                            Map<String, Object> map = new HashMap<>();//进班
                            map.put("dktime", starttime_one);
                            map.put("name", "进班");
                            map.put("attend", (qk == 1 ? "进班缺卡" : "") + (e == 1 ? "进班正常" : "") + (b == 1 ? "进班迟到" : ""));
                            map.put("attendTime", "".equals(jTime) ? "" : (DateUtil.getdaytime(jTime, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map);
                            Map<String, Object> map2 = new HashMap<>();//离班
                            map2.put("dktime", endtime_two);
                            map2.put("name", "离班");
                            map2.put("attend", (f == 1 ? "离班正常" : "") + (d == 1 ? "离班早退" : ""));
                            map2.put("attendTime", "".equals(lTime) ? "" : (DateUtil.getdaytime(lTime, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map2);
                            sMap.put("state", index);
                            sMap.put("list", maps);
                        }
                    } else {
                        //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                        long l = DateUtil.getcompTime(endtimeOne, starttimeOne);
                        String s = DateUtil.addtimeMinutes(starttimeOne, (int) l / 2);
                        //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                        long l1 = DateUtil.getcompTime(starttimeTwo, endtimeOne);
                        String s1 = DateUtil.addtimeMinutes(endtimeOne, (int) l1 / 2);
                        //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                        long l2 = DateUtil.getcompTime(endtimeTwo, starttimeTwo);
                        String s3 = DateUtil.addtimeMinutes(starttimeTwo, (int) l2 / 2);
                        if (attendMap.get(id) != null && attendMap.get(id) != null && attendMap.size() > 0) {//今天没有考勤
                            String jTime = "";
                            String lTime = "";
                            String jTimeTwo = "";
                            String lTimeTwo = "";
                            int state = 0;
                            String[] split = attendMap.get(id).split(",");
                            for (int j = 0; j < split.length; j++) {
                                String attendtime = split[j];
                                if (DateUtil.valueCompTime(s, attendtime) >= 0) {//第一段考勤是否有考勤
                                    if (DateUtil.valueCompTime(starttimeOne, attendtime) >= 0) {
                                        e = 1;//"进班正常"
                                        jTime = attendtime;
                                    }
                                    if (DateUtil.valueCompTime(s, attendtime) > 0) {//"进班迟到"
                                        if (e == 0) {
                                            b = 1;
                                            state = 1;
                                            jTime = attendtime;
                                        }
                                    }
                                }

                                if (DateUtil.valueCompTime(s, attendtime) <= 0 && DateUtil.valueCompTime(s1, attendtime) >= 0) {
                                    if (DateUtil.valueCompTime(endtimeOne, attendtime) >= 0) {//"离班早退"
                                        if (f == 0) {
                                            d = 1;
                                            state = 1;
                                            lTime = attendtime;
                                        }
                                    }
                                    if (DateUtil.valueCompTime(endtimeOne, attendtime) < 0) {//"离班正常"
                                        if (d == 0) {
                                            f = 1;
                                            lTime = attendtime;
                                        }
                                    }
                                }

                                //第二段进班
                                if (DateUtil.valueCompTime(starttimeTwo, attendtime) >= 0 && DateUtil.valueCompTime(s1, attendtime) <= 0) {
                                    E = 1;//"进班正常"
                                    jTimeTwo = attendtime;
                                } else {
                                    if (DateUtil.valueCompTime(starttimeTwo, attendtime) < 0 && DateUtil.valueCompTime(s3, attendtime) > 0) {//"进班迟到"
                                        if (E == 0) {
                                            B = 1;
                                            state = 1;
                                            jTimeTwo = attendtime;
                                        }
                                    }
                                }

                                //第二段离班
                                if (DateUtil.valueCompTime(endtimeTwo, attendtime) >= 0 && DateUtil.valueCompTime(s3, attendtime) <= 0) {//"离班早退"
                                    D = 1;
                                    state = 1;
                                    lTimeTwo = attendtime;
                                } else {
                                    if (DateUtil.valueCompTime(endtimeTwo, attendtime) < 0) {//"离班正常
                                        if (D == 0) {
                                            F = 1;
                                            state = 1;
                                            lTimeTwo = attendtime;
                                        }
                                    }
                                }
                            }
                            if (e == 0 && b == 0 && DateUtil.valueCompTime(gettime, s) > 0) {
                                a = 1;//"进班缺卡"
                                state = 1;
                            }
                            if (d == 0 && f == 0 && DateUtil.valueCompTime(gettime, s1) > 0) {
                                c = 1;//"离班缺卡"
                                state = 1;
                            }

                            if (E == 0 && B == 0 && DateUtil.valueCompTime(gettime, s3) > 0) {
                                A = 1;//"进班缺卡"
                                state = 1;
                            }

                            Map<String, Object> map = new HashMap<>();
                            map.put("dktime", starttime_one);
                            map.put("name", "进班");
                            map.put("attend", (a == 1 ? "进班缺卡" : "") + (e == 1 ? "进班正常" : "") + (b == 1 ? "进班迟到" : ""));
                            map.put("attendTime", "".equals(jTime) ? "" : (DateUtil.getdaytime(jTime, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map);
                            Map<String, Object> map2 = new HashMap<>();
                            map2.put("dktime", endtime_one);
                            map2.put("name", "离班");
                            map2.put("attend", (c == 1 ? "离班缺卡" : "") + (d == 1 ? "离班早退" : "") + (f == 1 ? "离班正常" : ""));
                            map2.put("attendTime", "".equals(lTime) ? "" : (DateUtil.getdaytime(lTime, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map2);
                            Map<String, Object> map3 = new HashMap<>();
                            map3.put("dktime", starttime_two);
                            map3.put("name", "进班");
                            map3.put("attend", (A == 1 ? "进班缺卡" : "") + (E == 1 ? "进班正常" : "") + (B == 1 ? "进班迟到" : ""));
                            map3.put("attendTime", "".equals(jTimeTwo) ? "" : (DateUtil.getdaytime(jTimeTwo, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map3);
                            Map<String, Object> map4 = new HashMap<>();
                            map4.put("dktime", endtime_two);
                            map4.put("name", "离班");
                            map4.put("attend", (D == 1 ? "离班早退" : "") + (F == 1 ? "离班正常" : ""));
                            map4.put("attendTime", "".equals(lTimeTwo) ? "" : (DateUtil.getdaytime(lTimeTwo, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map4);
                            sMap.put("state", state);
                            sMap.put("list", maps);
                        } else {
                            int index = 0;
                            if (DateUtil.valueCompTime(DateUtil.time(), s) > 0) {
                                index++;
                            }

                            if (DateUtil.valueCompTime(DateUtil.time(), s1) > 0) {
                                index++;
                            }

                            if (DateUtil.valueCompTime(DateUtil.time(), s3) > 0) {
                                index++;
                            }
                            Map<String, Object> map = new HashMap<>();
                            map.put("dktime", starttime_one);
                            map.put("name", "进班");
                            map.put("attend", index > 0 ? "进班缺卡" : "");
                            maps.add(map);
                            Map<String, Object> map2 = new HashMap<>();
                            map2.put("dktime", endtime_one);
                            map2.put("name", "离班");
                            map2.put("attend", index > 1 ? "离班缺卡" : "");
                            maps.add(map2);
                            Map<String, Object> map3 = new HashMap<>();
                            map3.put("dktime", starttime_two);
                            map3.put("name", "进班");
                            map3.put("attend", index > 2 ? "进班缺卡" : "");
                            maps.add(map3);
                            Map<String, Object> map4 = new HashMap<>();
                            map4.put("dktime", endtime_two);
                            map4.put("name", "离班");
                            maps.add(map4);
                            sMap.put("state", index > 0 ? 1 : 0);
                            sMap.put("list", maps);
                        }
                    }
                }
            }
        } else {
            sql = "SELECT shift_id  string FROM class_attendlist WHERE attend_group_id=?  AND TO_DAYS(opertime)=TO_DAYS(?)  LIMIT 1";
            String shift_id = attendJdbc.queryString(sql, new Object[]{attend_groupid, dateTime});

            if (StringUtils.isEmpty(shift_id) || "0".equals(shift_id)) {//查询的当天为休息
                for (int i = 0; i < studentList.size(); i++) {
                    Map<String, Object> sMap = studentList.get(i);
                    List<Map<String, Object>> maps = new ArrayList<>();
                    Map<String, Object> shiftMap = new HashMap<>();
                    shiftMap.put("attend", "休息");
                    maps.add(shiftMap);
                    sMap.put("state", 0);
                    sMap.put("list", maps);
                }
            } else {
                sql = " SELECT  GROUP_CONCAT( t.attendtype  ORDER BY sort  ) attend,t.userid FROM class_attendlist t WHERE   " +
                        "  t.attend_group_id=?   AND TO_DAYS(t.opertime)=TO_DAYS(?)  GROUP BY t.userid ";
                Map<String, String> attendMap = null;
                try {
                    attendMap = attendJdbc.queryAttendMap(sql, attend_groupid, dateTime);
                } catch (Exception e) {
                    logger.info("考勤2.0 没有考勤数据。。");
                }

                sql = " SELECT  GROUP_CONCAT( t.attendtime  ORDER BY sort  ) attend,t.userid FROM class_attendlist t WHERE   " +
                        "  t.attend_group_id=?   AND TO_DAYS(t.opertime)=TO_DAYS(?)  GROUP BY t.userid ";
                Map<String, String> timeMap = null;
                try {
                    timeMap = attendJdbc.queryAttendMap(sql, attend_groupid, dateTime);
                } catch (Exception e) {
                    logger.info("考勤2.0 没有考勤数据。。");
                }

                sql = "SELECT  type,starttime_one,endtime_one, starttime_two,endtime_two  FROM attend_shift WHERE id=? ";
                List<String> timeList = attendJdbc.queryAttendList(sql, shift_id);
                for (int i = 0; i < studentList.size(); i++) {
                    Map<String, Object> sMap = studentList.get(i);
                    String id = sMap.get("id") + "";
                    List<Map<String, Object>> maps = new ArrayList<>();
                    int a = 0;
                    //考勤类型: 0请假 1进班正常 2进班迟到 3进班缺卡  4离班正常 5离班早退  6离班缺卡 7休息 8缺勤
                    if (attendMap != null && attendMap.size() > 0 && attendMap.get(id) != null) {
                        String[] split = attendMap.get(id).split(",");
                        for (int i1 = 0; i1 < split.length; i1++) {
                            String s1 = split[i1];
                            Map<String, Object> shiftMap = new HashMap<>();
                            shiftMap.put("dktime", timeList.get(i1));
                            shiftMap.put("name", i1 % 2 == 0 ? "进班" : "离班");
                            if (s1 != null || s1.equals("2") || s1.equals("3") || s1.equals("5") || s1.equals("6") || s1.equals("8")) {
                                a = 1;
                            }
                            shiftMap.put("attend", getAttendType(s1));
                            if (timeMap.get(id).split(",").length > i1) {
                                shiftMap.put("attendTime", (!"3".equals(s1) && !"6".equals(s1) && !"8".equals(s1)) ? DateUtil.getdaytime(timeMap.get(id).split(",")[i1], "yyyy-MM-dd HH:mm:ss", "HH:mm") : "");
                            }
                            maps.add(shiftMap);
                        }
                    }
                    sMap.put("state", a);
                    sMap.put("list", maps);
                }
            }
        }
        return studentList;
    }

    @Override
    public List<Map<String, Object>> queryStudentAttend(String attend_groupid, String school_id, String dateTime, String classId) {
        String[] times = new String[4];
        String sql = "SELECT shift_id,attend_group_type,opertime FROM attendance_group WHERE id=?";
        Map<String, Object> groupObjectMap = null;
        try {
            groupObjectMap = attendJdbc.queryMap(sql, attend_groupid);
        } catch (Exception e) {
            logger.info("queryStudentAttend-->groupObjectMap,error={}", e);
        }

        if (groupObjectMap == null) {
            return null;
        }
        String shiftid = (String) groupObjectMap.get("shift_id");//班次id列
        int type = (int) groupObjectMap.get("attend_group_type");//0学生考勤、1教师考勤
        String opertime = (String) groupObjectMap.get("opertime");
        String s = "";
        if (!StringUtils.isEmpty(classId)) {//按班级筛选
            s = " AND t.class_id in (" + classId + ") ";
        }
        List<Map<String, Object>> userList = null;
        if (type == 0) {
            String sql3 = "SELECT t.id ,t.student_name ,t.head_portrait FROM attendance_group_user u,student t " +
                    "   LEFT JOIN classes s  ON t.class_id=s.id AND s.school_id=?  WHERE u.attend_group_id=? AND t.id=u.userid" + s;
            userList = attendJdbc.queryListMap(sql3, school_id, attend_groupid);
        }
//0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
        //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息
        if (userList == null) {
            return null;
        }
        if (DateUtil.getDay(dateTime, opertime) > 0) {
            String sql2 = "SELECT shift_id  ints FROM attend_his WHERE attend_group_id=?  AND TO_DAYS(opertime)=TO_DAYS(?)  LIMIT 1";
            Integer integer = attendJdbc.queryInteger(sql2, attend_groupid, dateTime);
            sql2 = "SELECT*FROM attendance_shift WHERE id=?";
            Map<String, Object> shiftMap = null;
            try {
                shiftMap = attendJdbc.queryMap(sql2, integer);
            } catch (Exception e) {
                logger.info("queryStudentAttend-->map1,error={}", e);
            }
            if (shiftMap != null) {
                int type1 = (int) shiftMap.get("type");
                if (type1 == 0) {//考勤类型:0 1天1次、1 1天2次
                    times[0] = shiftMap.get("starttime").toString();
                    times[1] = shiftMap.get("endtime1").toString();
                } else {
                    times[0] = shiftMap.get("starttime").toString();
                    times[1] = shiftMap.get("endtime").toString();
                    times[2] = shiftMap.get("starttime1").toString();
                    times[3] = shiftMap.get("endtime1").toString();
                }
            }
            for (int i = 0; i < userList.size(); i++) {
                Map<String, Object> map = userList.get(i);
                Object id = map.get("id");
                String sql1 = " SELECT * FROM attend_his WHERE attend_group_id=? AND userid=? AND TO_DAYS(opertime)=TO_DAYS(?)";
                Map<String, Object> map1 = null;
                try {
                    map1 = attendJdbc.queryMap(sql1, attend_groupid, id, dateTime);
                } catch (Exception e) {
                    logger.info("queryStudentAttend-->map1,error={}", e);
                }
                if (map1 != null) {
                    String attendtype = (String) map1.get("attendtype");
                    String time = (String) map1.get("opertime");
                    String attendtime = (String) map1.get("attendtime");
                    int a = 0;
                    map.put("state", 0);
                    String[] split = attendtype.split(",");
                    String[] split1 = attendtime.split(",");
                    ArrayList<Map<String, Object>> maps = new ArrayList<>();
                    //异常状态
                    for (int j = 0; j < split.length; j++) {
                        Map<String, Object> map2 = new HashMap<>();
                        String s1 = split[j];
                        if (!"0".equals(s1) && !"1".equals(s1) && !"2".equals(s1) && !"6".equals(s1) && !"8".equals(s1) && !"12".equals(s1) && !"15".equals(s1)) {
                            a++;
                        }
                        String attend = getAttend(s1);
                        map2.put("attend", attend);
                        map2.put("dateTime", time);
                        map2.put("week", DateUtil.getWeekEnglish(time));
                        map2.put("zh_CN_week", DateUtil.getWeeks(time));
                        if (j < split1.length) {
                            if (!"".equals(split1[j]) && !"''".equals(split1[j])) {
                                String getdaytime = DateUtil.getdaytime(split1[j], "yyyy-MM-dd HH:mm:ss", "HH:mm");
                                map2.put("attendTime", getdaytime);
                            }
                        }
                        maps.add(map2);
                    }
                    if (a > 0) {
                        map.put("state", 1);
                    }
                    map.put("list", maps);
                }
            }
        } else {
            String[] split = shiftid.split(",");
            int weekInt = DateUtil.getWeekInt(dateTime);
            String s1 = split[weekInt - 1];
            logger.info("s1={}", s1);
            if ("0".equals(s1)) {
                for (int i = 0; i < userList.size(); i++) {
                    Map<String, Object> map = userList.get(i);
                    ArrayList<Map<String, Object>> maps = new ArrayList<>();
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("attend", "休息");
                    map2.put("dateTime", DateUtil.getdaytime(dateTime));
                    map2.put("week", DateUtil.getWeekEnglish(dateTime));
                    map2.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                    map.put("state", 0);
                    maps.add(map2);
                    map.put("list", maps);
                }
            } else {
                String sql1 = "SELECT*FROM attendance_shift WHERE id=?";
                Map<String, Object> map1 = null;
                try {
                    map1 = attendJdbc.queryMap(sql1, s1);
                } catch (Exception e) {
                    logger.info("queryStudentAttend-->map1,error={}", e);
                }
                if (map1 != null) {
                    int type1 = (int) map1.get("type");
                    Map<String, Object> map4 = new HashMap<>();
                    if (type1 == 0) {//考勤类型:0 1天1次、1 1天2次
                        times[0] = map1.get("starttime").toString();
                        times[1] = map1.get("endtime1").toString();
                    } else {
                        times[0] = map1.get("starttime").toString();
                        times[1] = map1.get("endtime").toString();
                        times[2] = map1.get("starttime1").toString();
                        times[3] = map1.get("endtime1").toString();
                    }
                    for (int i = 0; i < userList.size(); i++) {
                        Map<String, Object> map = userList.get(i);
                        Object id = map.get("id");
                        String sql2 = "SELECT o.userid tid,o.attend_group_id,o.shift_id,o.attendtime,o.attendtype FROM attend o WHERE o.school_id=? AND o.userid=? " +
                                " AND o.attend_group_id=? AND o.state=0 AND TO_DAYS(o.attendtime)=TO_DAYS(?)  GROUP BY attendtime ORDER BY attendtime ";
                        List<Map<String, Object>> attendList = attendJdbc.queryAttendListMap(sql2, school_id, id, attend_groupid, dateTime);
                        Map<String, Object> map2 = attendList(attendList, map1, dateTime, id);
                        map.putAll(map2);
                    }
                }
            }
        }
        for (int i = 0; i < userList.size(); i++) {
            Map<String, Object> map = userList.get(i);
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
            if (list != null && list.size() > 0) {
                for (int i1 = 0; i1 < list.size(); i1++) {
                    Map<String, Object> map1 = list.get(i1);
                    if (!"休息".equals(map1.get("attend").toString()) && !"缺勤".equals(map1.get("attend").toString())) {
                        map1.put("dktime", times[i1]);
                        map1.put("name", i1 % 2 == 0 ? "进班" : "离班");
                    }
                }
            }
        }
        return userList;
    }


    /**
     * @描述 统计考勤数量
     * @创建人 zm
     * @创建时间 2020/5/13
     */
    @Override
    public Result queryStudentAttendCount(String attend_groupid, String school_id, String dateTime, String classId) {
        String sql = "SELECT shift_id,attend_group_type,opertime FROM attendance_group WHERE id=?";
        Map<String, Object> groupObjectMap = null;
        try {
            groupObjectMap = attendJdbc.queryMap(sql, attend_groupid);
        } catch (Exception e) {
            logger.info("queryStudentAttend-->groupObjectMap,error={}", e);
        }

        if (groupObjectMap == null) {
            return null;
        }
        String shiftid = (String) groupObjectMap.get("shift_id");//班次id列
        int type = (int) groupObjectMap.get("attend_group_type");//0学生考勤、1教师考勤
        String opertime = (String) groupObjectMap.get("opertime");
        String s = "";
        if (!StringUtils.isEmpty(classId)) {//按班级筛选
            s = " AND t.class_id in (" + classId + ") ";
        }
        List<Map<String, Object>> userList = null;
        if (type == 0) {
            String sql3 = "SELECT t.id ,t.student_name ,t.head_portrait FROM attendance_group_user u,student t " +
                    "   LEFT JOIN classes s  ON t.class_id=s.id AND s.school_id=?  WHERE u.attend_group_id=? AND t.id=u.userid" + s;
            userList = attendJdbc.queryListMap(sql3, school_id, attend_groupid);
        }
//0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
        //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息 16未打卡
        if (userList == null) {
            return null;
        }

        if (DateUtil.getDay(dateTime, opertime) > 0) {
            sql = " SELECT SUM(IF( h.attendtype=2 || h.attendtype=8 || h.attendtype=6 || h.attendtype=12,1,0)) zccount, " +
                    " SUM(IF( h.attendtype=3 || h.attendtype=9,1,0)) cdcount,SUM(IF( h.attendtype=4 || h.attendtype=10 || h.attendtype=7 || h.attendtype=13,1,0)) qkcount,SUM(IF( h.attendtype=5 || h.attendtype=11,1,0)) ztcount," +
                    " SUM(IF( h.attendtype=14,1,0)) qqcount  FROM  (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( h.attendtype, ',', b.help_topic_id + 1   ), ',', - 1 ) AS  attendtype   " +
                    " FROM attend_his h JOIN mysql.help_topic b    ON b.help_topic_id < (LENGTH(h.attendtype) - LENGTH(REPLACE (h.attendtype, ',', '')) + 1    )     WHERE h.attend_group_id=? AND h.userid IN   " +
                    " ( SELECT t.id  FROM attendance_group_user u,student t  LEFT JOIN classes s  ON t.class_id=s.id AND s.school_id=?  WHERE u.attend_group_id=? AND t.id=u.userid) " +
                    " AND TO_DAYS(h.opertime)=TO_DAYS(?)  ) h ";
            Map<String, Object> map = null;
            try {
                map = attendJdbc.queryMap(sql, attend_groupid, school_id, attend_groupid, dateTime);
            } catch (Exception e) {
            }
            return new Result(map);
        } else {
            String[] split = shiftid.split(",");
            int weekInt = DateUtil.getWeekInt(dateTime);
            String s1 = split[weekInt - 1];
            logger.info("s1={}", s1);
            if ("0".equals(s1)) {//休息直接处理
                return new Result();
            } else {
                String sql1 = "SELECT*FROM attendance_shift WHERE id=?";
                Map<String, Object> map1 = null;
                try {
                    map1 = attendJdbc.queryMap(sql1, s1);
                } catch (Exception e) {
                    logger.info("queryStudentAttend-->map1,error={}", e);
                }
                if (map1 != null) {
                    List<Map<String, Integer>> maps = new ArrayList<>();
                    for (int i = 0; i < userList.size(); i++) {
                        Map<String, Object> map = userList.get(i);
                        Object id = map.get("id");
                        String sql2 = " SELECT o.userid tid,o.attend_group_id,o.shift_id,o.attendtime,o.attendtype FROM attend o WHERE o.school_id=? AND o.userid=? " +
                                " AND o.attend_group_id=? AND o.state=0 AND TO_DAYS(o.attendtime)=TO_DAYS(?)  GROUP BY attendtime ORDER BY attendtime ";
                        List<Map<String, Object>> attendList = attendJdbc.queryAttendListMap(sql2, school_id, id, attend_groupid, dateTime);
                        List<Map<String, Integer>> intList = attendIntList(attendList, map1, dateTime, id);
                        maps.addAll(intList);
                    }
                    return getAttendCount(maps);
                }
            }
        }
        return new Result();
    }


    //    考勤2.0
    @Override
    public Result queryStudentAttendCount2(String school_id, String dateTime, Integer type) {
        String gettime = DateUtil.time();
        List<Map<String, Object>> list = new ArrayList<>();
        if (DateUtil.getDay(gettime, dateTime) != 0) {
            String sql = "SELECT  IFNULL(SUM(IF(c.attendtype=1||c.attendtype=4,1,0)),0) zccount,IFNULL(SUM(IF(c.attendtype=2,1,0)),0) " +
                    "cdcount,IFNULL(SUM(IF(c.attendtype=3||c.attendtype=6||c.attendtype=8,1,0)),0) qkcount,IFNULL(SUM(IF(c.attendtype=5,1,0)),0) " +
                    " ztcount  FROM class_attendlist c,student t,attend_group g,attend_group_user u   WHERE u.userid=t.id AND " +
                    " c.attend_group_id=g.id AND u.attend_group_id=g.id AND c.userid=u.userid AND g.school_id=? AND TO_DAYS(c.opertime)=TO_DAYS(?) AND " +
                    "(c.sort=? OR c.attendtype=IF(( SELECT  type  FROM attend_shift WHERE id=c.shift_id)>0,8,-1)) ";

            String attendsql = "SELECT  IFNULL(SUM(IF(c.attendtype=1||c.attendtype=4,1,0)),0) zccount,IFNULL(SUM(IF(c.attendtype=2,1,0)),0) " +
                    "cdcount,IFNULL(SUM(IF(c.attendtype=3||c.attendtype=6||c.attendtype=8,1,0)),0) qkcount,IFNULL(SUM(IF(c.attendtype=5,1,0)),0) " +
                    " ztcount  FROM class_attendlist c,student t,attend_group g,attend_group_user u   WHERE u.userid=t.id AND " +
                    " c.attend_group_id=g.id AND u.attend_group_id=g.id AND c.userid=u.userid AND g.id=? AND TO_DAYS(c.opertime)=TO_DAYS(?) AND " +
                    "(c.sort=? OR c.attendtype=IF(( SELECT  type  FROM attend_shift WHERE id=c.shift_id)>0,8,-1)) ";

            String gSql = "SELECT id gid,name gname FROM attend_group WHERE school_id=?";
            List<Map<String, Object>> gList = attendJdbc.queryListMap(gSql, school_id);
            if (gList != null && gList.size() > 0) {
                for (int i = 0; i < gList.size(); i++) {
                    Map<String, Object> map = gList.get(i);
                    Map<String, Object> aMap = null;
                    try {
                        aMap = attendJdbc.queryMap(attendsql, map.get("g.id"), dateTime, type - 1);
                    } catch (Exception e) {

                    }
                    map.put("attendMap", aMap);
                }
            }
            Map<String, Object> sMap = null;
            try {
                sMap = attendJdbc.queryMap(sql, school_id, dateTime, type - 1);
            } catch (Exception e) {

            }
            Map<String, Object> map = new HashMap<>();
            map.put("gid", 0);
            map.put("gname", "全校数据");
            map.put("attendMap", sMap);
            list.add(map);
            list.addAll(gList);
            return new Result(list);
        } else {
            int weeksInt = DateUtil.getWeeksInt(gettime);
            String jSql = "  SELECT SUM(IF(h.attendtime<=?,1,0)) zccount,IFNULL(SUM(IF(h.attendtime IS NULL || h.attendtime<=? ,0,1)),0) cdcount, " +
                    " IFNULL(SUM(IF(h.attendtime IS NULL&&now()>?,1,0)),0) qkcount,0 ztcount FROM  (attend_group_user u,student t) LEFT JOIN  " +
                    "  (SELECT MIN(h.attendtime) attendtime,h.userid ,h.attend_group_id FROM   class_attendlist h WHERE  h.attend_group_id=? " +
                    "    AND h.attendtime>=? AND h.attendtime<=? GROUP BY userid  ) h  " +
                    "    ON (t.id=h.userid AND h.attend_group_id=u.attend_group_id  ) WHERE t.id=u.userid AND u.attend_group_id=?  ";

            String lSql = " SELECT SUM(IF(h.attendtime>=?,1,0)) zccount,0 cdcount,  IFNULL(SUM(IF(h.attendtime IS NULL&&now()>?,1,0)),0) qkcount ," +
                    " IFNULL(SUM(IF(h.attendtime IS NULL || h.attendtime>=? ,0,1)),0) ztcount FROM  (attend_group_user u,student t) LEFT JOIN  " +
                    "  (SELECT MIN(h.attendtime) attendtime,h.userid ,h.attend_group_id FROM   class_attendlist h WHERE  h.attend_group_id=? " +
                    "  AND h.attendtime>? AND h.attendtime<=? GROUP BY userid  ) h  " +
                    "  ON (t.id=h.userid AND h.attend_group_id=u.attend_group_id  ) WHERE t.id=u.userid AND u.attend_group_id=? ";

            String sql = "SELECT IF(shift_id!=0,shift_id,(SELECT shift_id FROM attend_group_shift WHERE attend_group_id=attend_group.id AND sort=? ) ) shift_id,id gid,name gname FROM attend_group WHERE school_id=?";
            List<Map<String, Object>> attendlist = attendJdbc.queryListMap(sql, weeksInt, school_id);//获取所有考勤组
            String sqlShift = "SELECT  `type`, `starttime_one`, `endtime_one`, `starttime_two`, `endtime_two` FROM attend_shift WHERE id=?"; //查询班次
            int zccount = 0;//正常
            int cdcount = 0;//迟到
            int qkcount = 0;//缺卡
            int ztcount = 0;//早退
            if (attendlist != null && attendlist.size() > 0) {
                for (int i = 0; i < attendlist.size(); i++) {
                    Map<String, Object> map = attendlist.get(i);
                    String shift_id = map.get("shift_id") + "";
                    Object gid = map.get("gid");
                    if (!"0".equals(shift_id) && !"-1".equals(shift_id)) {//今天非休息日
                        Map<String, Object> timeMap = null;
                        try {
                            timeMap = attendJdbc.queryMap(sqlShift, shift_id);
                        } catch (Exception e) {
                            logger.info("executeInternal2.0-->erorr={}", e);
                        }
                        int attendType = (int) timeMap.get("type");//考勤类型:0 1天1次、1 1天2次
                        String starttimeOne = DateUtil.AddTime(gettime, timeMap.get("starttime_one") + "");
                        String endtimeOne = DateUtil.AddTime(gettime, timeMap.get("endtime_one") + "");
                        String starttimeTwo = DateUtil.AddTime(gettime, timeMap.get("starttime_two") + "");
                        String endtimeTwo = DateUtil.AddTime(gettime, timeMap.get("endtime_two") + "");
                        if (attendType == 0) {
                            String time = "";
                            if (!StringUtils.isEmpty(endtimeOne) && !StringUtils.isEmpty(starttimeTwo)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                                long l = DateUtil.getcompTime(starttimeTwo, endtimeOne);
                                time = DateUtil.addtimeMinutes(endtimeOne, (int) l / 2);
                            } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                                long l = DateUtil.getcompTime(endtimeTwo, starttimeOne);
                                time = DateUtil.addtimeMinutes(starttimeOne, (int) l / 2);
                            }
                            Map<String, Object> maps = null;
                            try {
                                if (type == 1) {
                                    maps = attendJdbc.queryMap(jSql, starttimeOne, starttimeOne, time, gid, DateUtil.gettime(), time, gid);
                                    map.put("attendMap", maps);
                                } else if (type == 2) {
                                    String s = DateUtil.addDays(DateUtil.gettime(), 1);
                                    maps = attendJdbc.queryMap(lSql, endtimeTwo, s, endtimeTwo, gid, time, s, gid);
                                    map.put("attendMap", maps);
                                }
                            } catch (Exception e) {
                                logger.info("考勤2.0", e);
                            }
                            if (maps != null) {
                                zccount += Integer.parseInt(maps.get("zccount").toString());
                                cdcount += Integer.parseInt(maps.get("cdcount").toString());
                                qkcount += Integer.parseInt(maps.get("qkcount").toString());
                                ztcount += Integer.parseInt(maps.get("ztcount").toString());
                            }
                        } else {
                            //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                            long l = DateUtil.getcompTime(endtimeOne, starttimeOne);
                            String s = DateUtil.addtimeMinutes(starttimeOne, (int) l / 2);
                            //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                            long l1 = DateUtil.getcompTime(starttimeTwo, endtimeOne);
                            String s1 = DateUtil.addtimeMinutes(endtimeOne, (int) l1 / 2);
                            //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                            long l2 = DateUtil.getcompTime(endtimeTwo, starttimeTwo);
                            String s3 = DateUtil.addtimeMinutes(starttimeTwo, (int) l2 / 2);
                            Map<String, Object> maps = null;
                            try {
                                if (type == 1) {
                                    maps = attendJdbc.queryMap(jSql, starttimeOne, starttimeOne, s, gid, DateUtil.gettime(), s, gid);
                                    map.put("attendMap", maps);
                                } else if (type == 2) {
                                    maps = attendJdbc.queryMap(lSql, endtimeOne, s1, endtimeOne, gid, s, s1, gid);
                                    map.put("attendMap", maps);
                                } else if (type == 3) {
                                    maps = attendJdbc.queryMap(jSql, starttimeTwo, starttimeTwo, s3, gid, s1, s3, gid);
                                    map.put("attendMap", maps);
                                } else if (type == 4) {
                                    String s2 = DateUtil.addDays(DateUtil.gettime(), 1);
                                    maps = attendJdbc.queryMap(lSql, endtimeTwo, s2, endtimeTwo, gid, s3, s2, gid);
                                    map.put("attendMap", maps);
                                }
                            } catch (Exception e) {
                                logger.info("考勤2.0", e);
                            }
                            if (maps != null) {
                                zccount += Integer.parseInt(maps.get("zccount").toString());
                                cdcount += Integer.parseInt(maps.get("cdcount").toString());
                                qkcount += Integer.parseInt(maps.get("qkcount").toString());
                                ztcount += Integer.parseInt(maps.get("ztcount").toString());
                            }
                        }
                    }
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("gid", 0);
            map.put("gname", "全校数据");
            Map<String, Object> attendMap = new HashMap<>();
            attendMap.put("zccount", zccount);
            attendMap.put("cdcount", cdcount);
            attendMap.put("qkcount", qkcount);
            attendMap.put("ztcount", ztcount);
            map.put("attendMap", attendMap);
            list.add(map);
            list.addAll(attendlist);
            return new Result(list);
        }
    }


    @Override
    public Result querySchoolAttendCount(String school_id, String dateTime, Integer type) {
        int weekInt = DateUtil.getWeekInt(dateTime);
        String sql = "SELECT t.id gid,t.name gname,t.opertime,t.id gid,a.id sid FROM attendance_shift a, ( SELECT  SUBSTRING_INDEX(SUBSTRING_INDEX(shift_id,',',?),',',-1) sid,name,id,opertime FROM attendance_group WHERE school_id=? GROUP BY id ORDER BY id ) t WHERE a.id=t.sid ";
        List<Map<String, Object>> list = attendJdbc.queryListMap(sql, weekInt, school_id);
        if (list == null || list.size() <= 0) {
            return null;
        }
        List<Map<String, Object>> lists = new ArrayList<>();
        sql = " SELECT SUM(IF( m.attendtype=2 || m.attendtype=8 || m.attendtype=6 || m.attendtype=12,1,0)) zccount, SUM(IF( m.attendtype=3 || m.attendtype=9,1,0)) cdcount,SUM(IF( m.attendtype=4 || m.attendtype=14 || m.attendtype=10 || m.attendtype=7 || m.attendtype=13,1,0)) qkcount,SUM(IF( m.attendtype=5 || m.attendtype=11,1,0)) ztcount " +
                " FROM (SELECT  SUBSTRING_INDEX(SUBSTRING_INDEX(h.attendtype,',',?),',',-1) attendtype FROM (attendance_group_user u,student t) LEFT JOIN attend_his h ON " +
                " (u.userid=h.userid AND u.attend_group_id=h.attend_group_id AND h.attend_group_id=?  AND h.shift_id=? AND TO_DAYS(h.opertime)=TO_DAYS(?))   WHERE  u.userid=t.id AND u.attend_group_id=? GROUP BY t.id ) m";

        int zccount = 0;//正常
        int cdcount = 0;//迟到
        int qkcount = 0;//缺卡
        int ztcount = 0;//早退

        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> maps = list.get(i);
            String opertime = maps.get("opertime").toString();//考勤组时间
            String attend_groupid = maps.get("gid").toString();//考勤组id
            String sid = maps.get("sid").toString();//班次id
            if (DateUtil.getDay(dateTime, opertime) > 0) {
                logger.info("DateUtil.getDay(dateTime, opertime) > 0");
                Map<String, Object> map = null;
                try {
                    map = attendJdbc.queryMap(sql, type, attend_groupid, sid, dateTime, attend_groupid);
                    if (map != null) {
                        zccount += Integer.parseInt(map.get("zccount").toString());
                        cdcount += Integer.parseInt(map.get("cdcount").toString());
                        qkcount += Integer.parseInt(map.get("qkcount").toString());
                        ztcount += Integer.parseInt(map.get("ztcount").toString());
                    }
                } catch (Exception e) {
                    logger.info("querySchoolAttendCount-->map1,error={}", e);
                }
                maps.put("attendMap", map);
            } else {
                String sql1 = "SELECT*FROM attendance_shift WHERE id=?";
                Map<String, Object> stringStringMap = null;
                try {
                    stringStringMap = attendJdbc.queryMap(sql1, sid);
                } catch (Exception e) {
                    logger.info("queryStudentAttend-->map1,error={}", e);
                }
                String stype = stringStringMap.get("type") + "";//考勤类型:0 1天1次、1 1天2次
                String starttime = stringStringMap.get("starttime") + "";
                starttime = DateUtil.AddTime(dateTime, starttime);
                String endtime = stringStringMap.get("endtime") + "";
                endtime = DateUtil.AddTime(dateTime, endtime);
                String starttime1 = stringStringMap.get("starttime1") + "";
                starttime1 = DateUtil.AddTime(dateTime, starttime1);
                String endtime1 = stringStringMap.get("endtime1") + "";
                endtime1 = DateUtil.AddTime(dateTime, endtime1);
                logger.info("attendString==>starttime={},endtime={},starttime1={},endtime1={}", starttime, endtime, starttime1, endtime1);
                if ("0".equals(stype)) {
                    String s = "";
                    if (!StringUtils.isEmpty(endtime) && !StringUtils.isEmpty(starttime1)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                        long l = DateUtil.getcompTime(starttime1, endtime);
                        s = DateUtil.addtimeMinutes(endtime, (int) l / 2);
                    } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                        long l = DateUtil.getcompTime(endtime1, starttime);
                        s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                    }
                    String sql2 = "   SELECT SUM(IF(h.attendtime<=?,1,0)) zccount,SUM(IF(h.attendtime<=?,0,1)) cdcount,if(h.id is null,1,0) qkcount,0 ztcount FROM  (attendance_group_user u,student t) LEFT JOIN " +
                            " attend h ON (t.id=h.userid  AND u.attend_group_id=h.attend_group_id AND h.state=0 " +
                            " AND  h.attend_group_id=? AND h.shift_id=?  AND h.attendtime>=? " +
                            "                    AND h.attendtime<=? )  WHERE t.id=u.userid AND u.attend_group_id=? ";

                    String sql3 = "   SELECT SUM(IF(h.attendtime>=?,1,0)) zccount,0 cdcount,if(h.id is null,1,0) qkcount,SUM(IF(h.attendtime>=?,0,1)) ztcount FROM  (attendance_group_user u,student t) LEFT JOIN " +
                            " attend h ON (t.id=h.userid  AND u.attend_group_id=h.attend_group_id AND h.state=0 " +
                            " AND  h.attend_group_id=? AND h.shift_id=?  AND h.attendtime>? " +
                            "                    AND h.attendtime<=? )  WHERE t.id=u.userid AND u.attend_group_id=? ";

                    if (type == 1) {
                        String s1 = DateUtil.addDays(dateTime, 0);
                        Map<String, Object> map = null;
                        try {
                            map = attendJdbc.queryMap(sql2, starttime, starttime, attend_groupid, sid, s1, s, attend_groupid);
                            if (map != null) {
                                zccount += Integer.parseInt(map.get("zccount").toString());
                                cdcount += Integer.parseInt(map.get("cdcount").toString());
                                qkcount += Integer.parseInt(map.get("qkcount").toString());
                                ztcount += Integer.parseInt(map.get("ztcount").toString());
                            }
                        } catch (Exception e) {
                        }
                        maps.put("attendMap", map);
                    } else if (type == 2) {
                        String s1 = DateUtil.addDays(dateTime, 1);
                        Map<String, Object> map = null;
                        try {
                            map = attendJdbc.queryMap(sql3, endtime1, endtime1, attend_groupid, sid, s, s1, attend_groupid);
                            if (map != null) {
                                zccount += Integer.parseInt(map.get("zccount").toString());
                                cdcount += Integer.parseInt(map.get("cdcount").toString());
                                qkcount += Integer.parseInt(map.get("qkcount").toString());
                                ztcount += Integer.parseInt(map.get("ztcount").toString());
                            }
                        } catch (Exception e) {
                        }
                        maps.put("attendMap", map);
                    }
                } else {
                    //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                    long l = DateUtil.getcompTime(endtime, starttime);
                    String s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                    //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                    long l1 = DateUtil.getcompTime(starttime1, endtime);
                    String s1 = DateUtil.addtimeMinutes(endtime, (int) l1 / 2);
                    //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                    long l2 = DateUtil.getcompTime(endtime1, starttime1);
                    String s3 = DateUtil.addtimeMinutes(starttime1, (int) l2 / 2);
                    String sql2 = "   SELECT SUM(IF(h.attendtime<=?,1,0)) zccount,SUM(IF(h.attendtime  IS NULL || h.attendtime<=?,0,1)) cdcount,SUM(if(h.id is null,1,0)) qkcount,0 ztcount FROM  (attendance_group_user u,student t) LEFT JOIN " +
                            " attend h ON (t.id=h.userid  AND u.attend_group_id=h.attend_group_id AND h.state=0 " +
                            " AND  h.attend_group_id=? AND h.shift_id=?  AND h.attendtime>=? " +
                            "                    AND h.attendtime<=? )  WHERE t.id=u.userid AND u.attend_group_id=? ";

                    String sql3 = "   SELECT SUM(IF(h.attendtime>=?,1,0)) zccount,0 cdcount,SUM(if(h.id is null,1,0)) qkcount,SUM( IF(h.attendtime  IS NULL || h.attendtime>=?,0,1)) ztcount FROM  (attendance_group_user u,student t) LEFT JOIN " +
                            " attend h ON (t.id=h.userid  AND u.attend_group_id=h.attend_group_id AND h.state=0 " +
                            " AND  h.attend_group_id=? AND h.shift_id=?  AND h.attendtime>? " +
                            "                    AND h.attendtime<=? )  WHERE t.id=u.userid AND u.attend_group_id=? ";

                    if (type == 1) {
                        String s2 = DateUtil.addDays(dateTime, 0);
                        Map<String, Object> map = null;
                        try {
                            map = attendJdbc.queryMap(sql2, starttime, starttime, attend_groupid, sid, s2, s, attend_groupid);
                            if (map != null) {
                                zccount += Integer.parseInt(map.get("zccount").toString());
                                cdcount += Integer.parseInt(map.get("cdcount").toString());
                                qkcount += Integer.parseInt(map.get("qkcount").toString());
                                ztcount += Integer.parseInt(map.get("ztcount").toString());
                            }
                        } catch (Exception e) {
                        }
                        maps.put("attendMap", map);
                    } else if (type == 2) {
                        Map<String, Object> map = null;
                        try {
                            map = attendJdbc.queryMap(sql3, endtime, endtime, attend_groupid, sid, s, s1, attend_groupid);
                            if (map != null) {
                                zccount += Integer.parseInt(map.get("zccount").toString());
                                cdcount += Integer.parseInt(map.get("cdcount").toString());
                                qkcount += Integer.parseInt(map.get("qkcount").toString());
                                ztcount += Integer.parseInt(map.get("ztcount").toString());
                            }
                        } catch (Exception e) {
                        }
                        maps.put("attendMap", map);
                    } else if (type == 3) {
                        Map<String, Object> map = null;
                        try {
                            map = attendJdbc.queryMap(sql2, starttime1, starttime1, attend_groupid, sid, s1, s3, attend_groupid);
                            if (map != null) {
                                zccount += Integer.parseInt(map.get("zccount").toString());
                                cdcount += Integer.parseInt(map.get("cdcount").toString());
                                qkcount += Integer.parseInt(map.get("qkcount").toString());
                                ztcount += Integer.parseInt(map.get("ztcount").toString());
                            }
                        } catch (Exception e) {
                        }
                        maps.put("attendMap", map);
                    } else if (type == 4) {
                        String s2 = DateUtil.addDays(dateTime, 1);
                        Map<String, Object> map = null;
                        try {
                            map = attendJdbc.queryMap(sql3, endtime1, endtime1, attend_groupid, sid, s3, s2, attend_groupid);
                            if (map != null) {
                                zccount += Integer.parseInt(map.get("zccount").toString());
                                cdcount += Integer.parseInt(map.get("cdcount").toString());
                                qkcount += Integer.parseInt(map.get("qkcount").toString());
                                ztcount += Integer.parseInt(map.get("ztcount").toString());
                            }
                        } catch (Exception e) {
                        }
                        maps.put("attendMap", map);
                    }
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("gid", 0);
        map.put("gname", "全校数据");
        map.put("sid", 0);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("zccount", zccount);
        map1.put("cdcount", cdcount);
        map1.put("qkcount", qkcount);
        map1.put("ztcount", ztcount);
        map.put("attendMap", map1);
        lists.add(map);
        lists.addAll(list);
        return new Result(lists);
    }


    @Override
    public Result queryTimeSlot(String schoolId, String dateTime) {
        ///int weekInt = DateUtil.getWeekInt(dateTime);
        //String sql = "SELECT type  FROM attendance_shift WHERE id IN ( SELECT GROUP_CONCAT(DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(shift_id,',',?),',',-1)) sid FROM attendance_group WHERE school_id=? ) ORDER BY TYPE DESC LIMIT 1";
        //考勤 2.0
        String sql = "SELECT type  FROM attend_shift WHERE id IN ( SELECT shift_id FROM class_attendlist WHERE  school_id=? AND TO_DAYS(opertime)=TO_DAYS(?) ) ORDER BY TYPE DESC LIMIT 1";
        Map<String, Object> map = null;
        try {
            map = attendJdbc.queryMap(sql, schoolId, dateTime);
        } catch (Exception e) {

        }
        if (map == null) {
            return new Result();
        }
        List<Map<String, Object>> list = new ArrayList<>();
        int type = (int) map.get("type");
        for (int i = 1; i < 3; i++) {
            Map<String, Object> maps = new HashMap<>();
            maps.put("type", i);
            maps.put("name", "第一段" + (i % 2 == 0 ? "离班" : "进班"));
            list.add(maps);
        }
        if (type == 1) {
            for (int i = 3; i < 5; i++) {
                Map<String, Object> maps = new HashMap<>();
                maps.put("type", i);
                maps.put("name", "第二段" + (i % 2 == 0 ? "离班" : "进班"));
                list.add(maps);
            }
        }
        return new Result(list);
    }


    public Result getAttendCount(List<Map<String, Integer>> intList) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        if (intList != null && intList.size() > 0) {
            logger.info("getAttendCount,intList={}", intList.toString());
            int zccount = 0;//正常
            int cdcount = 0;//迟到
            int qkcount = 0;//缺卡
            int ztcount = 0;//早退
            int qqcount = 0;//缺勤
            for (int i = 0; i < intList.size(); i++) {
                Integer integer = intList.get(i).get("attendtype");
                if (integer == 2 || integer == 8 || integer == 6 || integer == 12) {
                    zccount++;
                }
                if (integer == 3 || integer == 9) {
                    cdcount++;
                }

                if (integer == 4 || integer == 7 || integer == 10 || integer == 13) {
                    qkcount++;
                }

                if (integer == 5 || integer == 11) {
                    ztcount++;
                }

                if (integer == 14) {
                    qqcount++;
                }
            }
            map.put("zccount", zccount);
            map.put("cdcount", cdcount);
            map.put("qkcount", qkcount);
            map.put("ztcount", ztcount);
            map.put("qqcount", qqcount);
        }

        logger.info("getAttendCount,maps=={}", map.toString());
        return new Result(map);
    }

    /**
     * @描述 根据考勤组id查年级班级信息
     * @创建人 zm
     * @创建时间 2020/5/14
     */
    @Override
    public Result attendGroupClass(String attendGroupId, String schoolId) {
        List<Grade> grade = mapper.getGrade(attendGroupId, schoolId);
        return new Result(grade);
    }


    //0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
    //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息 16 未打卡
    public List<Map<String, Integer>> attendIntList
    (List<Map<String, Object>> attendList, Map<String, Object> stringStringMap, String s2, Object id) {
        List<Map<String, Integer>> hashMaps = new ArrayList<>();

        String getdaytime = DateUtil.getdaytime(s2);
        String type = stringStringMap.get("type") + "";//考勤类型:0 1天1次、1 1天2次
        String starttime = stringStringMap.get("starttime") + "";
        starttime = DateUtil.AddTime(getdaytime, starttime);
        String endtime = stringStringMap.get("endtime") + "";
        endtime = DateUtil.AddTime(getdaytime, endtime);
        String starttime1 = stringStringMap.get("starttime1") + "";
        starttime1 = DateUtil.AddTime(getdaytime, starttime1);
        String endtime1 = stringStringMap.get("endtime1") + "";
        endtime1 = DateUtil.AddTime(getdaytime, endtime1);
        logger.info("attendString==>starttime={},endtime={},starttime1={},endtime1={}", starttime, endtime, starttime1, endtime1);
        int a = 0;//"进班缺卡"
        int b = 0;//"进班迟到"
        int c = 0;//"离班缺卡"
        int d = 0;//"离班早退"
        int e = 0;//"进班正常"
        int f = 0;//"离班正常"

        //第二段
        int A = 0;//"进班缺卡"
        int B = 0;//"进班迟到"
        int C = 0;//"离班缺卡"
        int D = 0;//"离班早退"
        int E = 0;//"进班正常
        int F = 0;//"离班正常

        String sql = "SELECT  COUNT(1) count FROM  vacation WHERE  student_id=? AND  state=1 AND DATE(stime)<=DATE(?) AND DATE(etime)>= DATE(?)";
        if (attendJdbc.queryCount(sql, id, getdaytime, getdaytime) > 0) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("attendtype", 0);
            hashMaps.add(map);
            return hashMaps;
        }

        if (attendList == null || attendList.size() <= 0) {//如果今天没有考勤视为 缺勤
            HashMap<String, Integer> map = new HashMap<>();
            map.put("attendtype", 14);
            if (DateUtil.getDay(s2, DateUtil.time()) == 0) {
                map.put("attendtype", 16);
                if ("0".equals(type)) {
                    String s = "";
                    if (!StringUtils.isEmpty(endtime) && !StringUtils.isEmpty(starttime1)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                        long l = DateUtil.getcompTime(starttime1, endtime);
                        s = DateUtil.addtimeMinutes(endtime, (int) l / 2);
                    } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                        long l = DateUtil.getcompTime(endtime1, starttime);
                        s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                    }
                    if (DateUtil.valueCompTime(DateUtil.time(), s) > 0) {
                        map.put("attendtype", 4);
                    }
                } else {
                    //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                    long l = DateUtil.getcompTime(endtime, starttime);
                    String s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                    //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                    long l1 = DateUtil.getcompTime(starttime1, endtime);
                    String s1 = DateUtil.addtimeMinutes(endtime, (int) l1 / 2);
                    //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                    long l2 = DateUtil.getcompTime(endtime1, starttime1);
                    String s3 = DateUtil.addtimeMinutes(starttime1, (int) l2 / 2);
                    List<Map<String, Integer>> hashMaps1 = new ArrayList<>();
                    if (DateUtil.valueCompTime(DateUtil.time(), s) > 0) {
                        map.put("attendtype", 4);
                    }

                    if (DateUtil.valueCompTime(DateUtil.time(), s1) > 0 && DateUtil.valueCompTime(DateUtil.time(), s3) <= 0) {
                        hashMaps1.add(getMapInt(4));
                        hashMaps1.add(getMapInt(7));
                    }

                    if (DateUtil.valueCompTime(DateUtil.time(), s3) > 0) {
                        hashMaps1.add(getMapInt(4));
                        hashMaps1.add(getMapInt(7));
                        hashMaps1.add(getMapInt(10));
                    }
                    if (hashMaps1 != null && hashMaps1.size() > 0) {
                        return hashMaps1;
                    }
                }
            }
            hashMaps.add(map);
            return hashMaps;
        } else {
            if ("0".equals(type)) {//一天一段考勤
                String s = "";
                if (!StringUtils.isEmpty(endtime) && !StringUtils.isEmpty(starttime1)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                    long l = DateUtil.getcompTime(starttime1, endtime);
                    s = DateUtil.addtimeMinutes(endtime, (int) l / 2);
                } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                    long l = DateUtil.getcompTime(endtime1, starttime);
                    s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                }
                logger.info("attendString,s=={}", s);
                for (int i = 0; i < attendList.size(); i++) {//遍历这一天的考勤 进行判断
                    Map<String, Object> stringObjectMap = attendList.get(i);
                    String attendtime = (String) stringObjectMap.get("attendtime");
                    if (DateUtil.valueCompTime(starttime, attendtime) >= 0) {
                        e = 2;//"进班正常"
                    } else {
                        if (DateUtil.valueCompTime(s, attendtime) >= 0) {//"进班迟到"
                            if (e == 0) {
                                b = 3;
                            }
                        }
                    }


                    if (DateUtil.valueCompTime(s, attendtime) < 0 && DateUtil.valueCompTime(endtime1, attendtime) > 0) {//"离班早退"
                        if (f == 0) {
                            d = 5;
                        }
                    } else if (DateUtil.valueCompTime(endtime1, attendtime) <= 0) {//"离班正常"
                        if (d == 0) {
                            f = 6;
                        }
                    }

                    if (DateUtil.getDay(s2, DateUtil.gettime()) == 0 && DateUtil.valueCompTime(DateUtil.time(), s) <= 0 && d == 0 && f == 0) {//今天考勤时间特殊处理
                        d = -1;
                    }
                }

                if (e != 0) {
                    hashMaps.add(getMapInt(2));
                }

                if (b != 0 && b != -1) {
                    hashMaps.add(getMapInt(3));
                }

                if (e == 0 && b == 0) {
                    //"进班缺卡"
                    hashMaps.add(getMapInt(4));
                }

                if (d != 0 && d != -1) {
                    hashMaps.add(getMapInt(5));
                }

                if (f != 0) {
                    hashMaps.add(getMapInt(6));
                }

                if (d == 0 && f == 0 && DateUtil.getDay(s2, DateUtil.gettime()) != 0) {
                    //"离班缺卡"
                    hashMaps.add(getMapInt(7));
                }
                if (c == 7 && a == 4) {
                    List<Map<String, Integer>> hashMaps2 = new ArrayList<>();
                    hashMaps2.add(getMapInt(14));
                    return hashMaps2;
                }
                return hashMaps;
            } else {//一天两段考勤

                //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                long l = DateUtil.getcompTime(endtime, starttime);
                String s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                long l1 = DateUtil.getcompTime(starttime1, endtime);
                String s1 = DateUtil.addtimeMinutes(endtime, (int) l1 / 2);
                //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                long l2 = DateUtil.getcompTime(endtime1, starttime1);
                String s3 = DateUtil.addtimeMinutes(starttime1, (int) l2 / 2);
                for (int i = 0; i < attendList.size(); i++) {//遍历这一天的考勤 进行判断
                    Map<String, Object> stringObjectMap = attendList.get(i);
                    String attendtime = (String) stringObjectMap.get("attendtime");
                    //第一段进班
                    if (DateUtil.valueCompTime(starttime, attendtime) >= 0) {
                        e = 2;//"进班正常"
                    } else {
                        if (DateUtil.valueCompTime(s, attendtime) >= 0) {//"进班迟到"
                            if (e == 0) {
                                b = 3;
                            }
                        }
                    }

                    //第一段离班
                    if (DateUtil.valueCompTime(endtime, attendtime) > 0 && DateUtil.valueCompTime(s, attendtime) < 0) {//"离班早退"
                        if (f == 0) {
                            d = 5;
                        }
                    } else {
                        if (DateUtil.valueCompTime(s1, attendtime) >= 0 && DateUtil.valueCompTime(endtime, attendtime) <= 0) {//"离班正常
                            if (d == 0) {
                                f = 6;
                            }
                        }
                    }

                    if (DateUtil.getDay(s2, DateUtil.gettime()) == 0 && DateUtil.valueCompTime(DateUtil.time(), s1) <= 0 && d == 0 && f == 0) {//今天考勤时间特殊处理
                        d = -1;
                    }

                    //第二段进班
                    if (DateUtil.valueCompTime(starttime1, attendtime) >= 0 && DateUtil.valueCompTime(s1, attendtime) < 0) {
                        if (B == 0) {
                            E = 8;//"进班正常"
                        }
                    } else {
                        if (DateUtil.valueCompTime(starttime1, attendtime) < 0 && DateUtil.valueCompTime(s3, attendtime) >= 0) {//"进班迟到"
                            if (E == 0 || E == -1) {
                                B = 9;
                            }
                        }
                    }

                    if (DateUtil.getDay(s2, DateUtil.gettime()) == 0 && DateUtil.valueCompTime(DateUtil.time(), s3) <= 0 && E == 0 && B == 0) {//今天考勤时间特殊处理
                        E = -1;
                    }

                    //第二段离班
                    if (DateUtil.valueCompTime(endtime1, attendtime) > 0 && DateUtil.valueCompTime(s3, attendtime) < 0) {//"离班早退"
                        if (F == 0) {
                            D = 8;
                        }
                    } else {
                        if (DateUtil.valueCompTime(endtime1, attendtime) <= 0) {//"离班正常
                            if (D == 0) {
                                F = 12;
                            }
                        }
                    }
                }

                if (e != 0) {
                    hashMaps.add(getMapInt(2));
                }

                if (b != 0 && b != -1) {
                    hashMaps.add(getMapInt(3));
                }

                if (e == 0 && b == 0) {
                    hashMaps.add(getMapInt(4));
                }

                if (d != 0 && d != -1) {
                    hashMaps.add(getMapInt(5));
                }

                if (f != 0) {
                    hashMaps.add(getMapInt(6));
                }

                if (d == 0 && f == 0) {
                    hashMaps.add(getMapInt(7));
                }

                if (E != 0 && E != -1) {
                    hashMaps.add(getMapInt(8));
                }

                if (B != 0) {
                    hashMaps.add(getMapInt(9));
                }

                if (E == 0 && B == 0) {
                    //"离班缺卡"
                    hashMaps.add(getMapInt(10));
                }

                if (D != 0) {
                    hashMaps.add(getMapInt(11));
                }

                if (F != 0) {
                    hashMaps.add(getMapInt(12));
                }


                if (D == 0 && F == 0 && DateUtil.getDay(s2, DateUtil.gettime()) != 0) {
                    hashMaps.add(getMapInt(13));
                }
                if (c == 7 && a == 4 && C == 13 && A == 10) {
                    List<Map<String, Integer>> hashMaps2 = new ArrayList<>();
                    hashMaps2.add(getMapInt(14));
                    return hashMaps2;
                }
                return hashMaps;
            }
        }
    }


    //0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
    //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息
    public Map<String, Object> attendList
    (List<Map<String, Object>> attendList, Map<String, Object> stringStringMap, String s2, Object id) {

        Map<String, Object> map2 = new HashMap<>();
        List<Map<String, Object>> hashMaps = new ArrayList<>();

        String getdaytime = DateUtil.getdaytime(s2);
        String type = stringStringMap.get("type") + "";//考勤类型:0 1天1次、1 1天2次
        String starttime = stringStringMap.get("starttime") + "";
        starttime = DateUtil.AddTime(getdaytime, starttime);
        String endtime = stringStringMap.get("endtime") + "";
        endtime = DateUtil.AddTime(getdaytime, endtime);
        String starttime1 = stringStringMap.get("starttime1") + "";
        starttime1 = DateUtil.AddTime(getdaytime, starttime1);
        String endtime1 = stringStringMap.get("endtime1") + "";
        endtime1 = DateUtil.AddTime(getdaytime, endtime1);
        logger.info("attendString==>starttime={},endtime={},starttime1={},endtime1={}", starttime, endtime, starttime1, endtime1);
        int a = 0;//"进班缺卡"
        int b = 0;//"进班迟到"
        int c = 0;//"离班缺卡"
        int d = 0;//"离班早退"
        int e = 0;//"进班正常"
        int f = 0;//"离班正常"

        String a1 = "";
        String b1 = "";
        String c1 = "";
        String d1 = "";
        String e1 = "";
        String f1 = "";


        //第二段
        int A = 0;//"进班缺卡"
        int B = 0;//"进班迟到"
        int C = 0;//"离班缺卡"
        int D = 0;//"离班早退"
        int E = 0;//"进班正常
        int F = 0;//"离班正常


        String A1 = "";
        String B1 = "";
        String C1 = "";
        String D1 = "";
        String E1 = "";
        String F1 = "";

        int state = 0;
        String sql = "SELECT  COUNT(1) count FROM  vacation WHERE  student_id=? AND  state=1 AND DATE(stime)<=DATE(?) AND DATE(etime)>= DATE(?)";
        if (attendJdbc.queryCount(sql, id, getdaytime, getdaytime) > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("dateTime", DateUtil.getdaytime(s2));
            map.put("week", DateUtil.getWeekEnglish(s2));
            map.put("zh_CN_week", DateUtil.getWeeks(s2));
            map.put("attend", "请假");
            map2.put("state", 0);
            hashMaps.add(map);
            map2.put("list", hashMaps);
            return map2;
        }

        if (attendList == null || attendList.size() <= 0) {//如果今天没有考勤视为 缺勤
            HashMap<String, Object> map = new HashMap<>();
            map.put("dateTime", DateUtil.getdaytime(s2));
            map.put("week", DateUtil.getWeekEnglish(s2));
            map.put("zh_CN_week", DateUtil.getWeeks(s2));
            map.put("attend", queqin);
            map2.put("state", 1);
            if (DateUtil.getDay(s2, DateUtil.time()) == 0) {
                map.put("attend", weidaka);
                map2.put("state", 0);
                if ("0".equals(type)) {
                    String s = "";
                    if (!StringUtils.isEmpty(endtime) && !StringUtils.isEmpty(starttime1)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                        long l = DateUtil.getcompTime(starttime1, endtime);
                        s = DateUtil.addtimeMinutes(endtime, (int) l / 2);
                    } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                        long l = DateUtil.getcompTime(endtime1, starttime);
                        s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                    }
                    if (DateUtil.valueCompTime(DateUtil.time(), starttime) > 0) {
                        map2.put("state", 1);
                    }

                    if (DateUtil.valueCompTime(DateUtil.time(), s) > 0) {
                        map.put("attend", "进班缺卡");
                        map2.put("state", 1);
                    }

                } else {
                    //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                    long l = DateUtil.getcompTime(endtime, starttime);
                    String s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                    //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                    long l1 = DateUtil.getcompTime(starttime1, endtime);
                    String s1 = DateUtil.addtimeMinutes(endtime, (int) l1 / 2);
                    //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                    long l2 = DateUtil.getcompTime(endtime1, starttime1);
                    String s3 = DateUtil.addtimeMinutes(starttime1, (int) l2 / 2);
                    List<Map<String, Object>> hashMaps1 = new ArrayList<>();

                    if (DateUtil.valueCompTime(DateUtil.time(), starttime) > 0) {
                        map2.put("state", 1);
                    }

                    if (DateUtil.valueCompTime(DateUtil.time(), s) > 0) {
                        map.put("attend", "进班缺卡");
                        map2.put("state", 1);
                    }

                    if (DateUtil.valueCompTime(DateUtil.time(), s1) > 0 && DateUtil.valueCompTime(DateUtil.time(), s3) <= 0) {
                        hashMaps1.add(getMap(DateUtil.time(), "进班缺卡", null));
                        hashMaps1.add(getMap(DateUtil.time(), "离班缺卡", null));
                    }

                    if (DateUtil.valueCompTime(DateUtil.time(), s3) > 0) {
                        hashMaps1.add(getMap(DateUtil.time(), "进班缺卡", null));
                        hashMaps1.add(getMap(DateUtil.time(), "离班缺卡", null));
                        hashMaps1.add(getMap(DateUtil.time(), "进班缺卡", null));
                    }
                    if (hashMaps1 != null && hashMaps1.size() > 0) {
                        map2.put("state", 1);
                        map2.put("list", hashMaps1);
                        return map2;
                    }
                }
            }
            hashMaps.add(map);
            map2.put("list", hashMaps);
            return map2;
        } else {
            if ("0".equals(type)) {//一天一段考勤
                String s = "";
                if (!StringUtils.isEmpty(endtime) && !StringUtils.isEmpty(starttime1)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                    long l = DateUtil.getcompTime(starttime1, endtime);
                    s = DateUtil.addtimeMinutes(endtime, (int) l / 2);
                } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                    long l = DateUtil.getcompTime(endtime1, starttime);
                    s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                }
                logger.info("attendString,s=={}", s);
                for (int i = 0; i < attendList.size(); i++) {//遍历这一天的考勤 进行判断
                    Map<String, Object> stringObjectMap = attendList.get(i);
                    String attendtime = (String) stringObjectMap.get("attendtime");
                    if (DateUtil.valueCompTime(starttime, attendtime) >= 0) {
                        e = 2;//"进班正常"
                        e1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                    } else {
                        if (DateUtil.valueCompTime(s, attendtime) >= 0) {//"进班迟到"
                            if (e == 0) {
                                state = 1;
                                b = 3;
                                b1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                            }
                        }
                    }


                    if (DateUtil.valueCompTime(s, attendtime) < 0 && DateUtil.valueCompTime(endtime1, attendtime) > 0) {//"离班早退"
                        if (f == 0) {
                            d = 5;
                            state = 1;
                            d1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                        }
                    } else if (DateUtil.valueCompTime(endtime1, attendtime) <= 0) {//"离班正常"
                        if (d == 0) {
                            f = 6;
                            f1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                        }
                    }

                    if (DateUtil.getDay(s2, DateUtil.gettime()) == 0 && DateUtil.valueCompTime(DateUtil.time(), s) <= 0 && d == 0 && f == 0) {//今天考勤时间特殊处理
                        d = -1;
                    }
                }

                if (e != 0) {
                    hashMaps.add(getMap(s2, "进班正常", e1));
                }

                if (b != 0 && b != -1) {
                    state = 1;
                    hashMaps.add(getMap(s2, "进班迟到", b1));
                }

                if (e == 0 && b == 0) {
                    state = 1;
                    //"进班缺卡"
                    hashMaps.add(getMap(s2, "进班缺卡", null));
                }

                if (d != 0 && d != -1) {
                    state = 1;
                    hashMaps.add(getMap(s2, "离班早退", d1));
                }

                if (f != 0) {
                    hashMaps.add(getMap(s2, "离班正常", f1));
                }

                if (d == 0 && f == 0 && DateUtil.getDay(s2, DateUtil.gettime()) != 0) {
                    state = 1;
                    //"离班缺卡"
                    hashMaps.add(getMap(s2, "离班缺卡", null));
                }
                if (c == 7 && a == 4) {
                    List<Map<String, Object>> hashMaps2 = new ArrayList<>();
                    HashMap<String, Object> map3 = new HashMap<>();
                    HashMap<String, Object> map4 = new HashMap<>();
                    map3.put("dateTime", DateUtil.getdaytime(s2));
                    map3.put("week", DateUtil.getWeekEnglish(s2));
                    map3.put("zh_CN_week", DateUtil.getWeeks(s2));
                    map3.put("attend", "缺勤");
                    hashMaps2.add(map3);
                    map4.put("state", 1);
                    map4.put("list", hashMaps2);
                    return map4;
                }
                map2.put("list", hashMaps);
                map2.put("state", state);
                return map2;
            } else {//一天两段考勤

                //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                long l = DateUtil.getcompTime(endtime, starttime);
                String s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                long l1 = DateUtil.getcompTime(starttime1, endtime);
                String s1 = DateUtil.addtimeMinutes(endtime, (int) l1 / 2);
                //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                long l2 = DateUtil.getcompTime(endtime1, starttime1);
                String s3 = DateUtil.addtimeMinutes(starttime1, (int) l2 / 2);
                for (int i = 0; i < attendList.size(); i++) {//遍历这一天的考勤 进行判断
                    HashMap<String, Object> map1 = new HashMap<>();
                    map1.put("dateTime", DateUtil.getdaytime(s2));
                    map1.put("week", DateUtil.getWeekEnglish(s2));
                    map1.put("zh_CN_week", DateUtil.getWeeks(s2));
                    Map<String, Object> stringObjectMap = attendList.get(i);
                    String attendtime = (String) stringObjectMap.get("attendtime");
                    //第一段进班
                    if (DateUtil.valueCompTime(starttime, attendtime) >= 0) {
                        e = 2;//"进班正常"
                        e1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                    } else {
                        if (DateUtil.valueCompTime(s, attendtime) >= 0) {//"进班迟到"
                            if (e == 0) {
                                state = 1;
                                b = 3;
                                b1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                            }
                        }
                    }

                    //第一段离班
                    if (DateUtil.valueCompTime(endtime, attendtime) > 0 && DateUtil.valueCompTime(s, attendtime) < 0) {//"离班早退"
                        if (f == 0) {
                            state = 1;
                            d = 5;
                            d1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                        }
                    } else {
                        if (DateUtil.valueCompTime(s1, attendtime) >= 0 && DateUtil.valueCompTime(endtime, attendtime) <= 0) {//"离班正常
                            if (d == 0) {
                                f = 6;
                                f1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                            }
                        }
                    }

                    if (DateUtil.getDay(s2, DateUtil.gettime()) == 0 && DateUtil.valueCompTime(DateUtil.time(), s1) <= 0 && d == 0 && f == 0) {//今天考勤时间特殊处理
                        d = -1;
                    }

                    //第二段进班
                    if (DateUtil.valueCompTime(starttime1, attendtime) >= 0 && DateUtil.valueCompTime(s1, attendtime) < 0) {
                        if (B == 0) {
                            E = 8;//"进班正常"
                            E1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                        }
                    } else {
                        if (DateUtil.valueCompTime(starttime1, attendtime) < 0 && DateUtil.valueCompTime(s3, attendtime) >= 0) {//"进班迟到"
                            if (E == 0 || E == -1) {
                                state = 1;
                                B = 9;
                                B1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                            }
                        }
                    }

                    if (DateUtil.getDay(s2, DateUtil.gettime()) == 0 && DateUtil.valueCompTime(DateUtil.time(), s3) <= 0 && E == 0 && B == 0) {//今天考勤时间特殊处理
                        E = -1;
                    }

                    //第二段离班
                    if (DateUtil.valueCompTime(endtime1, attendtime) > 0 && DateUtil.valueCompTime(s3, attendtime) < 0) {//"离班早退"
                        if (F == 0) {
                            D = 8;
                            state = 1;
                            D1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                        }
                    } else {
                        if (DateUtil.valueCompTime(endtime1, attendtime) <= 0) {//"离班正常
                            if (D == 0) {
                                F = 12;
                                F1 = DateUtil.getdaytime(attendtime, "yyyy-MM-dd HH:mm:ss", "HH:mm");
                            }
                        }
                    }
                }

                if (e != 0) {
                    hashMaps.add(getMap(s2, "进班正常", e1));
                }

                if (b != 0 && b != -1) {
                    state = 1;
                    hashMaps.add(getMap(s2, "进班迟到", b1));
                }

                if (e == 0 && b == 0) {
                    state = 1;
                    hashMaps.add(getMap(s2, "进班缺卡", null));
                }

                if (d != 0 && d != -1) {
                    state = 1;
                    hashMaps.add(getMap(s2, "离班早退", d1));
                }

                if (f != 0) {
                    hashMaps.add(getMap(s2, "离班正常", f1));
                }

                if (d == 0 && f == 0) {//&& DateUtil.getDay(s2, DateUtil.gettime()) != 0
                    state = 1;
                    hashMaps.add(getMap(s2, "离班缺卡", null));
                }

                if (E != 0 && E != -1) {
                    hashMaps.add(getMap(s2, "进班正常", E1));
                }

                if (B != 0) {
                    state = 1;
                    hashMaps.add(getMap(s2, "进班迟到", B1));
                }

                if (E == 0 && B == 0) {
                    state = 1;
                    //"离班缺卡"
                    hashMaps.add(getMap(s2, "进班缺卡", null));
                }

                if (D != 0) {
                    hashMaps.add(getMap(s2, "离班早退", D1));
                }

                if (F != 0) {
                    state = 1;
                    hashMaps.add(getMap(s2, "离班正常", F1));
                }


                if (D == 0 && F == 0 && DateUtil.getDay(s2, DateUtil.gettime()) != 0) {
                    state = 1;
                    hashMaps.add(getMap(s2, "离班缺卡", null));
                }
                if (c == 7 && a == 4 && C == 13 && A == 10) {
                    List<Map<String, Object>> hashMaps2 = new ArrayList<>();
                    HashMap<String, Object> map3 = new HashMap<>();
                    HashMap<String, Object> map4 = new HashMap<>();
                    map3.put("dateTime", DateUtil.getdaytime(s2));
                    map3.put("week", DateUtil.getWeekEnglish(s2));
                    map3.put("zh_CN_week", DateUtil.getWeeks(s2));
                    map3.put("attend", "缺勤");
                    hashMaps2.add(map3);
                    map4.put("state", 1);
                    map4.put("list", hashMaps2);
                    return map4;
                }
                map2.put("list", hashMaps);
                map2.put("state", state);
                return map2;
            }
        }
    }

    public Map<String, Object> getMap(String s2, String attend, String time) {
        Map<String, Object> map1 = new HashMap<>();
        map1.put("dateTime", DateUtil.getdaytime(s2));
        map1.put("week", DateUtil.getWeekEnglish(s2));
        map1.put("zh_CN_week", DateUtil.getWeeks(s2));
        map1.put("attend", attend);
        map1.put("attendTime", time);
        return map1;
    }

    public Map<String, Integer> getMapInt(Integer attend) {
        Map<String, Integer> map1 = new HashMap<>();
        map1.put("attendtype", attend);
        return map1;
    }

    @Override
    public Result findStudentAttend(String classId, String dateTime, String schoolId) {

        //判断是否选择班级
        if (StringUtils.isEmpty(classId)) {
            return new Result();
        }
        //判断时间是否为空
        if (StringUtils.isEmpty(dateTime)) {
            //时间为空查询当天信息
            dateTime = DateUtil.gettime();
        }

        //按班级查询学生
        String sql = "select id,student_name,head_portrait from student where class_id= ?";

        //学生信息
        List<Map<String, Object>> studentList = null;

        try {
            studentList = attendJdbc.queryListMap(sql, classId);
        } catch (Exception e) {
            logger.error("没有学生信息 e", e);
        }

        //查询班级有的学生考勤组
        sql = "select ag.id,ag.name,ag.opertime,ag.shift_id shiftId from  attendance_group ag " +
                "LEFT JOIN attendance_group_classid agc on(ag.id=agc.attend_group_id)" +
                " where ag.school_id=? and agc.class_id=? and ag.attend_group_type=0";
        //考勤组信息
        List<Map<String, Object>> groupList = null;

        try {
            groupList = attendJdbc.queryListMap(sql, schoolId, classId);
        } catch (Exception e) {
            logger.error("没有考勤组 e", e);
        }
        //判断考勤组为空
        if (groupList == null || groupList.size() == 0) {
            return new Result(studentList);
        }


        //循环学生信息查询考勤数据
        for (Map<String, Object> studenMap : studentList) {
            studenMap.put("state", 0);
            logger.info("学生信息 map = {}", studenMap.toString());
            //返回的信息
            List<Map<String, Object>> list = new ArrayList<>();
            //重新赋值，防止数据累加
            List<Map<String, Object>> groupListTwo = new ArrayList<>();
            for (int f = 0; f < groupList.size(); f++) {
                Map<String, Object> groupMap = getMap(groupList.get(f));
                logger.info("考勤组信息 map = {}", groupMap.toString());
                //学生考勤数据SQL
                sql = "SELECT a.id,a.userid,a.attendtype,a.attendtime,DATE_FORMAT(a.attendtime,'%W') week,att.starttime,att.endtime,att.starttime1,att.endtime1,att.type FROM " +
                        "attend  a left JOIN attendance_shift  att on(a.shift_id=att.id) " +
                        "WHERE a.userid =?  AND a.attend_group_id =?  AND to_days( a.attendtime ) = to_days(?)  AND a.state = 0 and att.state=0 ORDER BY a.attendtype desc";
                //判断考勤组的修改时间过来有没有
                if (StringUtils.isEmpty(groupMap.get("opertime"))) {
                    logger.info("没有修改过");
                    //获取当天的考勤结果
                    List<Map<String, Object>> listMap = attendJdbc.queryListMap(sql, studenMap.get("id"), groupMap.get("id"), dateTime);

                    //判断是否没有考勤信息
                    if (StringUtils.isEmpty(listMap) || listMap.size() == 0) {
                        //groupMap.put("list", "当天无考勤信息");
                        //版次id
                        String shiftId = groupList.get(f).get("shiftId").toString().split(",")[DateUtil.getWeekInt(dateTime) - 1];
                        //查询考勤类型
                        String shiftSql = "select type string from attendance_shift where id = ?";
                        //查询考勤类型
                        String type = attendJdbc.queryString(shiftSql, shiftId);
                        list = judgeAttendance2(studenMap, type, dateTime);
                    } else {
                        //groupMap.put("list", judgeAttendance(listMap, dateTime));
                        list = judgeAttendance(listMap, dateTime, studenMap);
                    }


                } else {
                    logger.info("修改过");


                    //比如修改时间是2020-02-01,查询时间是2020-02-02此时为true
                    boolean result = DateUtil.compare(groupMap.get("opertime").toString(), dateTime, "yyyy-MM-dd");

                    //如果为true查询考勤表数据
                    if (result) {
                        //获取当天的考勤结果
                        List<Map<String, Object>> listMap = null;
                        try {
                            listMap = attendJdbc.queryListMap(sql, studenMap.get("id"), groupMap.get("id"), dateTime);
                        } catch (Exception e) {
                            logger.info("没有考勤信息");
                        }

                        //判断是否没有考勤信息
                        if (StringUtils.isEmpty(listMap) || listMap.size() == 0) {
                            //groupMap.put("list", "当天无考勤信息");
                            //版次id
                            String shiftId = groupList.get(f).get("shiftId").toString().split(",")[DateUtil.getWeekInt(dateTime) - 1];
                            //查询考勤类型
                            String shiftSql = "select type string from attendance_shift where id = ?";
                            //查询考勤类型
                            String type = attendJdbc.queryString(shiftSql, shiftId);
                            list = judgeAttendance2(studenMap, type, dateTime);
                        } else {
                            //groupMap.put("list", judgeAttendance(listMap, dateTime));
                            list = judgeAttendance(listMap, dateTime, studenMap);
                        }

                    } else {

                        //因为有修改过，并且查询的是历史记录的数据
                        sql = "select id,attendname,attendtime ,ah.attendtype ,DATE_FORMAT(ah.attendtime,'%W') week from attend_his ah where userid=? and attend_group_id = ? and to_days( opertime ) = to_days(?) ";
                        //获取当天的考勤结果
                        Map<String, Object> attendMap = null;

                        try {
                            attendMap = attendJdbc.queryMap(sql, studenMap.get("id"), groupMap.get("id"), dateTime);
                        } catch (Exception e) {
                            //创建map集合
                            Map<String, Object> attend = new HashMap<>();
                            attend.put("attendTime", "");
                            attend.put("week", "");
                            attend.put("dateTime", dateTime);
                            attend.put("attend", "无打卡");
                            attend.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                            list.add(attend);
                            continue;
                        }

                        //获取考勤时间
                        String[] attendtime = attendMap.get("attendtime").toString().split(",");
                        //获取考勤状态
                        String[] attendtype = attendMap.get("attendtype").toString().split(",");

                        //判断这条数据是否 请假，休息，跟缺勤
                        if (attendtype.length == 1) {
                            //循环考勤信息
                            for (int i = 0; i < attendtime.length; i++) {
                                //创建map集合
                                Map<String, Object> attend = new HashMap<>();
                                attend.put("attendTime", DateUtil.getdaytime(attendtime[i], "yyyy-MM-dd HH:mm:ss", "HH:mm"));
                                attend.put("week", "");
                                attend.put("dateTime", dateTime);
                                attend.put("attend", getAttend(attendtype[0]));
                                attend.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                                list.add(attend);
                            }
                        } else {
                            for (int i = 0; i < attendtime.length; i++) {

                                //0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
                                //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息
                                if (!"0".equals(attendtime[i]) ||
                                        !"1".equals(attendtime[i]) ||
                                        !"2".equals(attendtime[i]) ||
                                        !"6".equals(attendtime[i]) ||
                                        !"8".equals(attendtime[i]) ||
                                        !"12".equals(attendtime[i]) ||
                                        !"14".equals(attendtime[i]) ||
                                        !"15".equals(attendtime[i])) {
                                    studenMap.put("state", 1);
                                }
                                //创建map集合
                                Map<String, Object> attend = new HashMap<>();
                                attend.put("attendTime", DateUtil.getdaytime(attendtime[i], "yyyy-MM-dd HH:mm:ss", "HH:mm"));
                                attend.put("week", attendMap.get("week"));
                                attend.put("dateTime", dateTime);
                                attend.put("attend", getAttend(attendtype[i]));
                                attend.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                                list.add(attend);
                            }
                        }

                        // groupMap.put("list", list);
                    }
                }
                //groupListTwo.add(groupMap);
            }
            studenMap.put("list", list);
        }


        return new Result(studentList);
    }


    /**
     * @Description: 考勤 2.0
     * @Param:
     * @return:
     * @Author: zm
     * @Date: 2020/8/11
     */
    @Override
    public Map<String, Object> queryAttendByStudentId2(String id, String dateTime) {

        if (StringUtils.isEmpty(dateTime)) {
            dateTime = DateUtil.gettime();
        }

        //按班级查询学生
        String sql = "select id,student_name,head_portrait ,class_id,school_id from student where id= ?";

        //学生信息
        Map<String, Object> studenMap = null;

        try {
            studenMap = attendJdbc.queryMap(sql, id);
        } catch (Exception e) {
            logger.error("没有学生信息 e", e);
        }

        if (studenMap == null) {
            return null;
        }

        int id1 = (int) studenMap.get("id");
        String school_id = studenMap.get("school_id").toString();

        sql = "SELECT g.id,g.name FROM attend_group g ,attend_group_user u WHERE g.id=u.attend_group_id AND u.userid=?";
        List<Map<String, Object>> maps = attendJdbc.queryListMap(sql, id);
        if (maps == null || maps.size() <= 0) {
            return null;
        }
        for (int i = 0; i < maps.size(); i++) {
            Map<String, Object> map = maps.get(i);
            String s = map.get("id").toString();
            Map<String, Object> map1 = queryStudentAttend2(s, school_id, dateTime, id1);
            map.put("list", map1);
        }
        studenMap.put("list", maps);
        return studenMap;

    }

    @Override
    public Map<String, Object> queryAttendByStudentId(String id, String dateTime) {

        //按班级查询学生
        String sql = "select id,student_name,head_portrait ,class_id,school_id from student where id= ?";

        //学生信息
        Map<String, Object> studenMap = null;

        try {
            studenMap = attendJdbc.queryMap(sql, id);
        } catch (Exception e) {
            logger.error("没有学生信息 e", e);
        }

        if (studenMap == null) {
            return null;
        }

        int id1 = (int) studenMap.get("id");
        String school_id = studenMap.get("school_id").toString();

        sql = "SELECT g.id,g.name FROM attendance_group g ,attendance_group_user u WHERE g.id=u.attend_group_id AND u.userid=?";
        List<Map<String, Object>> maps = attendJdbc.queryListMap(sql, id);
        if (maps == null || maps.size() <= 0) {
            return null;
        }
        for (int i = 0; i < maps.size(); i++) {
            Map<String, Object> map = maps.get(i);
            String s = map.get("id").toString();
            Map<String, Object> map1 = queryStudentAttend1(s, school_id, dateTime, id1);
            map.put("list", map1);
        }
        studenMap.put("list", maps);
        return studenMap;

    }


    @Override
    public Result findAttendByStudentId(String id, String dateTime) {

        //判断时间是否为空
        if (StringUtils.isEmpty(dateTime)) {
            //时间为空查询当天信息
            dateTime = DateUtil.gettime();
        }

        //按班级查询学生
        String sql = "select id,student_name,head_portrait ,class_id from student where id= ?";

        //学生信息
        Map<String, Object> studenMap = null;

        try {
            studenMap = attendJdbc.queryMap(sql, id);
        } catch (Exception e) {
            logger.error("没有学生信息 e", e);
        }

        //查询班级有的学生考勤组
        sql = "select ag.id,ag.name,ag.opertime,ag.shift_id shiftId  from  attendance_group ag " +
                "LEFT JOIN attendance_group_classid agc on(ag.id=agc.attend_group_id)" +
                " where ag.school_id=1 and agc.class_id=? and ag.attend_group_type=0";
        //考勤组信息
        List<Map<String, Object>> groupList = null;

        try {
            groupList = attendJdbc.queryListMap(sql, studenMap.get("class_id"));
        } catch (Exception e) {
            logger.error("没有考勤组 e", e);
        }
        //判断考勤组为空
        if (groupList == null || groupList.size() == 0) {
            return new Result(studenMap);
        }

        studenMap.put("state", 0);
        logger.info("学生信息 map = {}", studenMap.toString());
        //返回的信息
        List<Map<String, Object>> list = new ArrayList<>();
        //重新赋值，防止数据累加
        List<Map<String, Object>> groupListTwo = new ArrayList<>();
        for (int f = 0; f < groupList.size(); f++) {
            Map<String, Object> groupMap = getMap(groupList.get(f));
            logger.info("考勤组信息 map = {}", groupMap.toString());
            //学生考勤数据SQL
            sql = "SELECT a.id,a.userid,a.attendtype,a.attendtime,DATE_FORMAT(a.attendtime,'%W') week,att.starttime,att.endtime,att.starttime1,att.endtime1,att.type FROM " +
                    "attend  a left JOIN attendance_shift  att on(a.shift_id=att.id) " +
                    "WHERE a.userid =?  AND a.attend_group_id =?  AND to_days( a.attendtime ) = to_days(?)  AND a.state = 0 and att.state=0 ORDER BY a.attendtype desc";
            //判断考勤组的修改时间过来有没有
            if (StringUtils.isEmpty(groupMap.get("opertime"))) {
                logger.info("没有修改过");
                //获取当天的考勤结果
                List<Map<String, Object>> listMap = attendJdbc.queryListMap(sql, studenMap.get("id"), groupMap.get("id"), dateTime);
                //判断是否没有考勤信息
                if (StringUtils.isEmpty(listMap) || listMap.size() == 0) {
                    // groupMap.put("list", "当天无考勤信息");
                    //版次id
                    String shiftId = groupList.get(f).get("shiftId").toString().split(",")[DateUtil.getWeekInt(dateTime) - 1];
                    //查询考勤类型
                    String shiftSql = "select type string from attendance_shift where id = ?";
                    //查询考勤类型
                    String type = attendJdbc.queryString(shiftSql, shiftId);
                    list = judgeAttendance2(studenMap, type, dateTime);
                } else {
                    //groupMap.put("list", );
                    list = judgeAttendance(listMap, dateTime, studenMap);
                }


            } else {
                logger.info("修改过");


                //比如修改时间是2020-02-01,查询时间是2020-02-02此时为true
                boolean result = DateUtil.compare(groupMap.get("opertime").toString(), dateTime, "yyyy-MM-dd");
                ;

                //如果为true查询考勤表数据
                if (result) {
                    //获取当天的考勤结果
                    List<Map<String, Object>> listMap = attendJdbc.queryListMap(sql, studenMap.get("id"), groupMap.get("id"), dateTime);
                    //判断是否没有考勤信息
                    if (StringUtils.isEmpty(listMap) || listMap.size() == 0) {
                        // groupMap.put("list", "当天无考勤信息");
                        //版次id
                        String shiftId = groupList.get(f).get("shiftId").toString().split(",")[DateUtil.getWeekInt(dateTime) - 1];
                        //查询考勤类型
                        String shiftSql = "select type string from attendance_shift where id = ?";
                        //查询考勤类型
                        String type = attendJdbc.queryString(shiftSql, shiftId);
                        list = judgeAttendance2(studenMap, type, dateTime);
                    } else {
                        //groupMap.put("list", );
                        list = judgeAttendance(listMap, dateTime, studenMap);
                    }
                } else {

                    //因为有修改过，并且查询的是历史记录的数据
                    sql = "select id,attendname,attendtime ,ah.attendtype ,DATE_FORMAT(ah.attendtime,'%W') week from attend_his ah where userid=? and attend_group_id = ? and to_days( opertime ) = to_days(?) ";
                    //获取当天的考勤结果
                    Map<String, Object> attendMap = null;

                    try {
                        attendMap = attendJdbc.queryMap(sql, studenMap.get("id"), groupMap.get("id"), dateTime);
                    } catch (Exception e) {
                        //创建map集合
                        Map<String, Object> attend = new HashMap<>();
                        attend.put("attendTime", "");
                        attend.put("week", attendMap.get(dateTime));
                        attend.put("dateTime", dateTime);
                        attend.put("attend", "无打卡");
                        attend.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                        list.add(attend);
                        continue;
                    }

                    //获取考勤时间
                    String[] attendtime = attendMap.get("attendtime").toString().split(",");
                    //获取考勤状态
                    String[] attendtype = attendMap.get("attendtype").toString().split(",");

                    //判断这条数据是否 请假，休息，跟缺勤
                    if (attendtype.length == 1) {
                        //循环考勤信息
                        for (int i = 0; i < attendtime.length; i++) {
                            //创建map集合
                            Map<String, Object> attend = new HashMap<>();
                            attend.put("attendTime", DateUtil.getdaytime(attendtime[i], "yyyy-MM-dd HH:mm:ss", "HH:mm"));
                            attend.put("week", attendMap.get("week"));
                            attend.put("dateTime", dateTime);
                            attend.put("attend", getAttend(attendtype[0]));
                            attend.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                            list.add(attend);
                        }
                    } else {
                        for (int i = 0; i < attendtime.length; i++) {

                            if (!"0".equals(attendtime[i]) ||
                                    !"1".equals(attendtime[i]) ||
                                    !"2".equals(attendtime[i]) ||
                                    !"6".equals(attendtime[i]) ||
                                    !"8".equals(attendtime[i]) ||
                                    !"12".equals(attendtime[i]) ||
                                    !"14".equals(attendtime[i]) ||
                                    !"15".equals(attendtime[i])) {
                                studenMap.put("state", 1);
                            }
                            //创建map集合
                            Map<String, Object> attend = new HashMap<>();
                            attend.put("attendTime", DateUtil.getdaytime(attendtime[i], "yyyy-MM-dd HH:mm:ss", "HH:mm"));
                            attend.put("week", attendMap.get("week"));
                            attend.put("dateTime", dateTime);
                            attend.put("attend", getAttend(attendtype[i]));
                            attend.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                            list.add(attend);
                        }
                    }
                    // groupMap.put("list", list);
                }
            }
            //groupListTwo.add(groupMap);
        }

        studenMap.put("list", list);
        return new Result(studenMap);
    }


    private Map<String, Object> getMap(Map<String, Object> stringObjectMap) {
        return new HashMap<String, Object>() {
            {
                put("id", stringObjectMap.get("id"));
                put("name", stringObjectMap.get("name"));
                put("opertime", stringObjectMap.get("opertime"));
            }
        };
    }


    /**
     * 查询学生当前时间的考勤信息整合
     *
     * @param listMap  考勤信息
     * @param dateTime 考勤信息
     * @return
     */
    public List<Map<String, Object>> judgeAttendance(List<Map<String, Object>> listMap, String
            dateTime, Map<String, Object> studenMap) {
        //0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
        //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息


        //判断是一段考勤还是两端考勤
        if ("0".equals(listMap.get(0).get("type").toString())) {
            //查询学生的考勤结果信息
            String sql = "select * from student_attendance_results sar where type =0 ";
            //循环教师信息
            for (Map<String, Object> map : listMap) {
                sql += " and  FIND_IN_SET('" + map.get("attendtype") + "',jie_guo)";
            }
            //查询只获取最后一条正确的数据
            sql += " GROUP BY id desc LIMIT 1";
            return get(sql, listMap, dateTime, studenMap);


        } else {
            //查询学生的考勤结果信息
            String sql = "select * from student_attendance_results sar where type =1 ";
            //循环教师信息
            for (Map<String, Object> map : listMap) {
                sql += " and  FIND_IN_SET('" + map.get("attendtype") + "',jie_guo)";
            }
            //查询只获取最后一条正确的数据
            sql += " GROUP BY id desc LIMIT 1";
            return get(sql, listMap, dateTime, studenMap);

        }
    }


    public List<Map<String, Object>> get(String sql, List<Map<String, Object>> listMap, String
            dateTime, Map<String, Object> studenMap) {

        //查询出要解析的值
        Map<String, Object> map = attendJdbc.queryMap(sql);
        //解析后要返会的值
        List<Map<String, Object>> returnValue = new ArrayList<>();
        //获取最终的考勤结果
        String jieGuo[] = map.get("jie_guo").toString().split(",");


        //是否打卡
        Boolean bool = true;
        //循环考勤结果
        for (int i = 0; i < jieGuo.length; i++) {
            //赋值
            Map<String, Object> mapThree = new HashMap<>();
            //循环考勤总条数
            for (Map<String, Object> mapTwo : listMap) {
                //判断结果是否相同，相同表示已经打卡
                if (jieGuo[i].equals(mapTwo.get("attendtype").toString())) {
                    mapThree.put("attendTime", DateUtil.getdaytime(mapTwo.get("attendtime").toString(), "yyyy-MM-dd HH:mm:ss", "HH:mm"));
                    mapThree.put("week", mapTwo.get("week"));
                    mapThree.put("dateTime", dateTime);
                    mapThree.put("attend", getAttend(mapTwo.get("attendtype").toString()));
                    mapThree.put("zh_CN_week", DateUtil.getWeeks(dateTime));

                    //已经打卡
                    bool = false;
                    break;
                }
            }
            if (bool) {
                //判断是否是本天，本天不计入缺卡
                if (DateUtil.getDay(DateUtil.gettime(), dateTime) == 0 || DateUtil.compare(DateUtil.gettime(), dateTime, "yyyy-MM-dd")) {
                    mapThree.put("attendTime", null);
                    mapThree.put("week", DateUtil.getWeeks(dateTime));
                    mapThree.put("dateTime", dateTime);
                    mapThree.put("attend", "未打卡");
                    mapThree.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                } else {
                    mapThree.put("attendTime", null);
                    mapThree.put("week", listMap.get(0).get("week"));
                    mapThree.put("dateTime", dateTime);
                    mapThree.put("attend", getAttend(jieGuo[i]));
                    mapThree.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                    studenMap.put("state", 1);
                }

            }
            returnValue.add(mapThree);
            //循环下一条结果时改为未打卡
            bool = true;
        }
        return returnValue;
    }


    private List<Map<String, Object>> judgeAttendance2(Map<String, Object> studenMap, String type, String
            dateTime) {

        //解析后要返会的值
        List<Map<String, Object>> returnValue = new ArrayList<>();

        //判断是一段考勤还是两端考勤(0是一段，1是两端)
        if ("0".equals(type)) {

            for (int i = 0; i < 2; i++) {
                //赋值
                Map<String, Object> mapThree = new HashMap<>();
                //判断是否是本天，本天不计入缺卡
                if (DateUtil.getDay(DateUtil.gettime(), dateTime) == 0 || DateUtil.compare(DateUtil.gettime(), dateTime, "yyyy-MM-dd")) {
                    mapThree.put("attendTime", null);
                    mapThree.put("week", DateUtil.getWeeks(dateTime));
                    mapThree.put("dateTime", dateTime);
                    mapThree.put("attend", "未打卡");
                    mapThree.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                } else {
                    mapThree.put("attendTime", null);
                    mapThree.put("week", DateUtil.getWeeks(dateTime));
                    mapThree.put("dateTime", dateTime);
                    mapThree.put("attend", i == 0 ? getAttend("4") : getAttend("7"));
                    mapThree.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                    studenMap.put("state", 1);
                }
                returnValue.add(mapThree);
            }

        } else {
            for (int i = 0; i < 4; i++) {
                //赋值
                Map<String, Object> mapThree = new HashMap<>();
                //判断是否是本天，本天不计入缺卡
                if (DateUtil.getDay(DateUtil.gettime(), dateTime) == 0 || DateUtil.compare(DateUtil.gettime(), dateTime, "yyyy-MM-dd")) {
                    mapThree.put("attendTime", null);
                    mapThree.put("week", DateUtil.getWeeks(dateTime));
                    mapThree.put("dateTime", dateTime);
                    mapThree.put("attend", "未打卡");
                    mapThree.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                } else {
                    mapThree.put("attendTime", null);
                    mapThree.put("week", DateUtil.getWeeks(dateTime));
                    mapThree.put("dateTime", dateTime);
                    if (0 == i) {
                        mapThree.put("attend", getAttend("4"));
                    } else if (1 == i) {
                        mapThree.put("attend", getAttend("7"));
                    } else if (2 == i) {
                        mapThree.put("attend", getAttend("10"));
                    } else {
                        mapThree.put("attend", getAttend("13"));
                    }
                    mapThree.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                    studenMap.put("state", 1);
                }
                returnValue.add(mapThree);
            }
        }
        return returnValue;
    }

    /**
     * 获取考勤结果
     *
     * @param type
     * @return
     */
    public String getAttend(String type) {
        //创建map 并且赋值
        Map<String, String> map = new HashMap<String, String>() {
            {
                //put("0", "病假");
                put("0", "请假");
                put("1", "事假");
                put("2", "进班正常");
                put("3", "进班迟到");
                put("4", "进班缺卡");
                put("5", "离班早退");
                put("6", "离班正常");
                put("7", "离班缺卡");
                put("8", "进班正常");
                put("9", "进班迟到");
                put("10", "进班缺卡");
                put("11", "离班早退");
                put("12", "离班正常");
                put("13", "离班缺卡");
                put("14", "缺勤");
                put("15", "休息");
                put("16", "未打卡");
            }
        };

        return map.get(type);
    }


    public String getAttend1(String type) {
        //创建map 并且赋值
        Map<String, String> map = new HashMap<String, String>() {
            {
                //put("0", "病假");
                put("0", "请假");
                put("1", "事假");
                put("2", "进班正常");
                put("3", "进班迟到");
                put("4", "进班缺卡");
                put("5", "离班早退");
                put("6", "离班正常");
                put("7", "离班缺卡");
                put("8", "第二段进班正常");
                put("9", "第二段进班迟到");
                put("10", "第二段进班缺卡");
                put("11", "第二段离班早退");
                put("12", "第二段离班正常");
                put("13", "第二段离班缺卡");
                put("14", "缺勤");
                put("15", "休息");
                put("16", "未打卡");
            }
        };

        return map.get(type);
    }

    /**
     * 英文星期转中文星期
     *
     * @param week
     * @return
     */
    public String getWeek(String week) {
        if ("Sunday".equals(week)) {
            return "周日";
        } else if ("Monday".equals(week)) {
            return "周一";
        } else if ("Tuesday".equals(week)) {
            return "周二";
        } else if ("Wednesday".equals(week)) {
            return "周三";
        } else if ("Thursday".equals(week)) {
            return "周四";
        } else if ("Friday".equals(week)) {
            return "周五";
        } else {
            return "周六";
        }
    }


    //考勤 2.0
    public Map<String, Object> queryStudentAttend2(String attend_groupid, String school_id, String dateTime, int tid) {
        Map<String, Object> stringObjectHashMap = new HashMap<>();
        String gettime = DateUtil.time();
        if (DateUtil.getDay(gettime, dateTime) == 0) {
            int weeksInt = DateUtil.getWeeksInt(gettime);
            String sql = "SELECT IF(shift_id!=0,shift_id,(SELECT shift_id FROM attend_group_shift WHERE attend_group_id=attend_group.id AND sort=? ) ) ints FROM attend_group WHERE school_id=? AND id=?";
            Integer shift_id = attendJdbc.queryInteger(sql, weeksInt, school_id, attend_groupid);
            if (shift_id == 0 || shift_id == -1) {//当天时间为休息
                List<Map<String, Object>> maps = new ArrayList<>();
                Map<String, Object> map = new HashMap<>();
                map.put("attend", "休息");
                maps.add(map);
                stringObjectHashMap.put("list", maps);
                return stringObjectHashMap;
            } else {
                sql = "SELECT attendtime   FROM class_attendlist WHERE attend_group_id=? AND userid=?  AND TO_DAYS(opertime)=TO_DAYS(?) order by attendtime";
                List<String> list = attendJdbc.queryListString(sql, new Object[]{attend_groupid, tid, dateTime});
                sql = "SELECT  f.type,f.starttime_one ,f.endtime_one ,f.starttime_two ,f.endtime_two   FROM   attend_shift f WHERE  f.id=?";
                Map<String, Object> shiftMap = null;
                try {
                    shiftMap = attendJdbc.queryMap(sql, shift_id);
                } catch (Exception e) {
                    logger.info("queryAttendUserStatusAll 2.0-->erorr={}", e);
                }
                if (shiftMap != null) {
                    int qk = 0;//当天 缺卡次数

                    int a = 0;//"进班缺卡"
                    int b = 0;//"进班迟到"
                    int c = 0;//"离班缺卡"
                    int d = 0;//"离班早退"
                    int e = 0;//"进班正常"
                    int f = 0;//"离班正常"


                    int A = 0;//"进班缺卡"
                    int B = 0;//"进班迟到"
                    int D = 0;//"离班早退"
                    int E = 0;//"进班正常
                    int F = 0;//"离班正常

                    String starttime_one = shiftMap.get("starttime_one") + "";
                    String endtime_one = shiftMap.get("endtime_one") + "";
                    String starttime_two = shiftMap.get("starttime_two") + "";
                    String endtime_two = shiftMap.get("endtime_two") + "";

                    String starttimeOne = DateUtil.AddTime(gettime, starttime_one);
                    String endtimeOne = DateUtil.AddTime(gettime, endtime_one);
                    String starttimeTwo = DateUtil.AddTime(gettime, starttime_two);
                    String endtimeTwo = DateUtil.AddTime(gettime, endtime_two);


                    int type1 = (int) shiftMap.get("type");
                    List<Map<String, Object>> maps = new ArrayList<>();
                    if (type1 == 0) {//考勤类型:0 1天1次、1 1天2次
                        String attendString = "未打卡";
                        String time = "";
                        if (!StringUtils.isEmpty(endtimeOne) && !StringUtils.isEmpty(starttimeTwo)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                            long l = DateUtil.getcompTime(starttimeTwo, endtimeOne);
                            time = DateUtil.addtimeMinutes(endtimeOne, (int) l / 2);
                        } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                            long l = DateUtil.getcompTime(endtimeTwo, starttimeOne);
                            time = DateUtil.addtimeMinutes(starttimeOne, (int) l / 2);
                        }
                        if (list != null && list.size() > 0) {

                            String jTime = "";
                            String lTime = "";

                            for (int i = 0; i < list.size(); i++) {
                                String attendtime = list.get(i);
                                if (DateUtil.valueCompTime(time, attendtime) >= 0) {//第一段考勤是否有考勤
                                    if (DateUtil.valueCompTime(starttimeOne, attendtime) >= 0) {
                                        e = 1;//"进班正常"
                                        jTime = attendtime;
                                    }
                                    if (DateUtil.valueCompTime(time, attendtime) > 0) {//"进班迟到"
                                        if (e == 0) {
                                            b = 1;
                                            jTime = attendtime;
                                        }
                                    }
                                }
                                if (DateUtil.valueCompTime(time, attendtime) <= 0 && DateUtil.valueCompTime(endtimeTwo, attendtime) >= 0) {//"离班早退"
                                    if (f == 0) {
                                        d = 1;
                                        lTime = attendtime;
                                    }
                                }
                                if (DateUtil.valueCompTime(endtimeTwo, attendtime) < 0) {//"离班正常"
                                    if (d == 0) {
                                        f = 1;
                                        lTime = attendtime;
                                    }
                                }
                            }
                            if (e == 0 && b == 0) {
                                qk = 1;//"缺卡"
                            }
                            Map<String, Object> map = new HashMap<>();//进班
                            map.put("dktime", starttime_one);
                            map.put("name", "进班");
                            map.put("attend", (qk == 1 ? "进班缺卡" : "") + (e == 1 ? "进班正常" : "") + (b == 1 ? "进班迟到" : ""));
                            map.put("attendTime", "".equals(jTime) ? "" : (DateUtil.getdaytime(jTime, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map);
                            Map<String, Object> map2 = new HashMap<>();//离班
                            map2.put("dktime", endtime_two);
                            map2.put("name", "离班");
                            map2.put("attend", (f == 1 ? "离班正常" : "") + (d == 1 ? "离班早退" : ""));
                            map2.put("attendTime", "".equals(lTime) ? "" : (DateUtil.getdaytime(lTime, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map2);
                            stringObjectHashMap.put("list", maps);
                        } else {//没有考勤
                            if (DateUtil.valueCompTime(DateUtil.time(), time) > 0) {
                                attendString = "进班缺卡";
                            }
                            Map<String, Object> map = new HashMap<>();
                            map.put("dktime", starttime_one);
                            map.put("name", "进班");
                            map.put("attend", attendString);
                            maps.add(map);
                            Map<String, Object> map2 = new HashMap<>();
                            map2.put("dktime", endtime_two);
                            map2.put("name", "离班");
                            maps.add(map2);
                            stringObjectHashMap.put("list", maps);
                        }
                        return stringObjectHashMap;
                    } else {
                        //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                        long l = DateUtil.getcompTime(endtimeOne, starttimeOne);
                        String s = DateUtil.addtimeMinutes(starttimeOne, (int) l / 2);
                        //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                        long l1 = DateUtil.getcompTime(starttimeTwo, endtimeOne);
                        String s1 = DateUtil.addtimeMinutes(endtimeOne, (int) l1 / 2);
                        //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                        long l2 = DateUtil.getcompTime(endtimeTwo, starttimeTwo);
                        String s3 = DateUtil.addtimeMinutes(starttimeTwo, (int) l2 / 2);
                        if (list != null && list.size() > 0) {//今天没有考勤
                            String jTime = "";
                            String lTime = "";
                            String jTimeTwo = "";
                            String lTimeTwo = "";

                            for (String attendtime : list) {
                                if (DateUtil.valueCompTime(s, attendtime) >= 0) {//第一段考勤是否有考勤
                                    if (DateUtil.valueCompTime(starttimeOne, attendtime) >= 0) {
                                        e = 1;//"进班正常"
                                        jTime = attendtime;
                                    }
                                    if (DateUtil.valueCompTime(s, attendtime) > 0) {//"进班迟到"
                                        if (e == 0) {
                                            b = 1;
                                            jTime = attendtime;
                                        }
                                    }
                                }

                                if (DateUtil.valueCompTime(s, attendtime) <= 0 && DateUtil.valueCompTime(s1, attendtime) >= 0) {
                                    if (DateUtil.valueCompTime(endtimeTwo, attendtime) > 0) {//"离班早退"
                                        if (f == 0) {
                                            d = 1;
                                            lTime = attendtime;
                                        }
                                    }
                                    if (DateUtil.valueCompTime(s1, attendtime) < 0) {//"离班正常"
                                        if (d == 0) {
                                            f = 1;
                                            lTime = attendtime;
                                        }
                                    }
                                }

                                //第二段进班
                                if (DateUtil.valueCompTime(starttimeTwo, attendtime) >= 0 && DateUtil.valueCompTime(s1, attendtime) <= 0) {
                                    E = 1;//"进班正常"
                                    jTimeTwo = attendtime;
                                } else {
                                    if (DateUtil.valueCompTime(starttimeTwo, attendtime) < 0 && DateUtil.valueCompTime(s3, attendtime) > 0) {//"进班迟到"
                                        if (E == 0) {
                                            B = 1;
                                            jTimeTwo = attendtime;
                                        }
                                    }
                                }

                                //第二段离班
                                if (DateUtil.valueCompTime(endtimeTwo, attendtime) >= 0 && DateUtil.valueCompTime(s3, attendtime) <= 0) {//"离班早退"
                                    D = 1;
                                    lTimeTwo = attendtime;
                                } else {
                                    if (DateUtil.valueCompTime(endtimeTwo, attendtime) < 0) {//"离班正常
                                        if (D == 0) {
                                            F = 1;
                                            lTimeTwo = attendtime;
                                        }
                                    }
                                }
                            }
                            if (e == 0 && b == 0 && DateUtil.valueCompTime(gettime, s) > 0) {
                                a = 1;//"进班缺卡"
                            }
                            if (d == 0 && f == 0 && DateUtil.valueCompTime(gettime, s1) > 0) {
                                c = 1;//"离班缺卡"
                            }

                            if (E == 0 && B == 0 && DateUtil.valueCompTime(gettime, s3) > 0) {
                                A = 1;//"进班缺卡"
                            }

                            Map<String, Object> map = new HashMap<>();
                            map.put("dktime", starttime_one);
                            map.put("name", "进班");
                            map.put("attend", (a == 1 ? "进班缺卡" : "") + (e == 1 ? "进班正常" : "") + (b == 1 ? "进班迟到" : ""));
                            map.put("attendTime", "".equals(jTime) ? "" : (DateUtil.getdaytime(jTime, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map);
                            Map<String, Object> map2 = new HashMap<>();
                            map2.put("dktime", endtime_one);
                            map2.put("name", "离班");
                            map2.put("attend", (c == 1 ? "离班缺卡" : "") + (d == 1 ? "离班早退" : "") + (f == 1 ? "离班正常" : ""));
                            map2.put("attendTime", "".equals(lTime) ? "" : (DateUtil.getdaytime(lTime, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map2);
                            Map<String, Object> map3 = new HashMap<>();
                            map3.put("dktime", starttime_two);
                            map3.put("name", "进班");
                            map3.put("attend", (A == 1 ? "进班缺卡" : "") + (E == 1 ? "进班正常" : "") + (B == 1 ? "进班迟到" : ""));
                            map3.put("attendTime", "".equals(jTimeTwo) ? "" : (DateUtil.getdaytime(jTimeTwo, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map3);
                            Map<String, Object> map4 = new HashMap<>();
                            map4.put("dktime", endtime_two);
                            map4.put("name", "离班");
                            map4.put("attend", (D == 1 ? "离班早退" : "") + (F == 1 ? "离班正常" : ""));
                            map4.put("attendTime", "".equals(lTimeTwo) ? "" : (DateUtil.getdaytime(lTimeTwo, "yyyy-MM-dd HH:mm:ss", "HH:mm")));
                            maps.add(map4);
                            stringObjectHashMap.put("list", maps);
                        } else {
                            int index = 0;
                            if (DateUtil.valueCompTime(DateUtil.time(), s) > 0) {
                                index++;
                            }

                            if (DateUtil.valueCompTime(DateUtil.time(), s1) > 0) {
                                index++;
                            }

                            if (DateUtil.valueCompTime(DateUtil.time(), s3) > 0) {
                                index++;
                            }
                            Map<String, Object> map = new HashMap<>();
                            map.put("dktime", starttime_one);
                            map.put("name", "进班");
                            map.put("attend", index > 0 ? "进班缺卡" : "");
                            maps.add(map);
                            Map<String, Object> map2 = new HashMap<>();
                            map2.put("dktime", endtime_one);
                            map2.put("name", "离班");
                            map2.put("attend", index > 1 ? "离班缺卡" : "");
                            maps.add(map2);
                            Map<String, Object> map3 = new HashMap<>();
                            map3.put("dktime", starttime_two);
                            map3.put("name", "进班");
                            map3.put("attend", index > 2 ? "进班缺卡" : "");
                            maps.add(map3);
                            Map<String, Object> map4 = new HashMap<>();
                            map4.put("dktime", endtime_two);
                            map4.put("name", "离班");
                            maps.add(map4);
                            stringObjectHashMap.put("list", maps);
                        }
                    }
                    return stringObjectHashMap;
                }
                return null;
            }
        } else {

            String sql = "SELECT  type,starttime_one,endtime_one, starttime_two,endtime_two  FROM attend_shift WHERE id=  " +
                    "(SELECT shift_id FROM class_attendlist WHERE attend_group_id=? AND userid=?  AND TO_DAYS(opertime)=TO_DAYS(?) LIMIT 1)";
            List<String> list = attendJdbc.queryAttendList(sql, attend_groupid, tid, dateTime);

            sql = "SELECT (CASE attendtype WHEN 0  THEN '请假' WHEN 1 THEN '进班正常' WHEN 2 THEN '进班迟到' WHEN 3 THEN '进班缺卡' WHEN 4 THEN '离班正常' WHEN 5 THEN '离班早退' WHEN 6 THEN '离班缺卡' WHEN 7 THEN '休息' WHEN 8 THEN '缺勤' END) attend,attendtime  attendTime,sort  FROM class_attendlist WHERE attend_group_id=? AND userid=?  AND TO_DAYS(opertime)=TO_DAYS(?) ORDER BY sort";
            List<Map<String, Object>> maps = attendJdbc.queryListMap(sql, attend_groupid, tid, dateTime);//查询当天的考勤 情况
            if (maps != null && maps.size() > 0) {
                for (int i = 0; i < maps.size(); i++) {
                    Map<String, Object> stringObjectMap = maps.get(i);
                    String s = "";
                    if (list != null && list.size() > 0 && i < list.size()) {
                        s = list.get(i);
                    }
                    stringObjectMap.put("dktime", s);
                    stringObjectMap.put("name", i % 2 == 0 ? "进班" : "离班");
                    String getdaytime = DateUtil.getdaytime(stringObjectMap.get("attendTime").toString(), "yyyy-MM-dd HH:mm:ss", "HH:mm");
                    stringObjectMap.put("attendTime", (!"缺勤".equals(stringObjectMap.get("attend") + "") && !"离班缺卡".equals(stringObjectMap.get("attend") + "") && !"进班缺卡".equals(stringObjectMap.get("attend") + "")) ? getdaytime : "");
                }
            }
            stringObjectHashMap.put("list", maps);
            return stringObjectHashMap;
        }
    }


    public Map<String, Object> queryStudentAttend1(String attend_groupid, String school_id, String dateTime, int tid) {
        String[] times = new String[4];
        String sql = "SELECT shift_id,attend_group_type,opertime FROM attendance_group WHERE id=?";
        Map<String, Object> groupObjectMap = null;
        try {
            groupObjectMap = attendJdbc.queryMap(sql, attend_groupid);
        } catch (Exception e) {
            logger.info("queryStudentAttend-->groupObjectMap,error={}", e);
        }

        if (groupObjectMap == null) {
            return null;
        }
        String shiftid = (String) groupObjectMap.get("shift_id");//班次id列
        int type = (int) groupObjectMap.get("attend_group_type");//0学生考勤、1教师考勤
        String opertime = (String) groupObjectMap.get("opertime");
        List<Map<String, Object>> userList = null;
        if (type == 0) {
            String sql3 = "SELECT t.id ,t.student_name ,t.head_portrait FROM attendance_group_user u,student t " +
                    "   LEFT JOIN classes s  ON t.class_id=s.id AND s.school_id=?  WHERE u.attend_group_id=? AND t.id=u.userid AND u.userid=? ";
            userList = attendJdbc.queryListMap(sql3, school_id, attend_groupid, tid);
        }
//0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
        //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息
        if (userList == null) {
            return null;
        }
        if (DateUtil.getDay(dateTime, opertime) > 0) {
            String sql2 = "SELECT shift_id  ints FROM attend_his WHERE attend_group_id=?  AND TO_DAYS(opertime)=TO_DAYS(?)  LIMIT 1";
            Integer integer = attendJdbc.queryInteger(sql2, attend_groupid, dateTime);
            sql2 = "SELECT*FROM attendance_shift WHERE id=?";
            Map<String, Object> shiftMap = null;
            try {
                shiftMap = attendJdbc.queryMap(sql2, integer);
            } catch (Exception e) {
                logger.info("queryStudentAttend-->map1,error={}", e);
            }
            if (shiftMap != null) {
                int type1 = (int) shiftMap.get("type");
                if (type1 == 0) {//考勤类型:0 1天1次、1 1天2次
                    times[0] = shiftMap.get("starttime").toString();
                    times[1] = shiftMap.get("endtime1").toString();
                } else {
                    times[0] = shiftMap.get("starttime").toString();
                    times[1] = shiftMap.get("endtime").toString();
                    times[2] = shiftMap.get("starttime1").toString();
                    times[3] = shiftMap.get("endtime1").toString();
                }
                ;
            }
            for (int i = 0; i < userList.size(); i++) {
                Map<String, Object> map = userList.get(i);
                Object id = map.get("id");
                String sql1 = " SELECT * FROM attend_his WHERE attend_group_id=? AND userid=? AND TO_DAYS(opertime)=TO_DAYS(?)";
                Map<String, Object> map1 = null;
                try {
                    map1 = attendJdbc.queryMap(sql1, attend_groupid, id, dateTime);
                } catch (Exception e) {
                    logger.info("queryStudentAttend-->map1,error={}", e);
                }
                if (map1 != null) {
                    String attendtype = (String) map1.get("attendtype");
                    String time = (String) map1.get("opertime");
                    String attendtime = (String) map1.get("attendtime");
                    int a = 0;
                    map.put("state", 0);
                    String[] split = attendtype.split(",");
                    String[] split1 = attendtime.split(",");
                    ArrayList<Map<String, Object>> maps = new ArrayList<>();
                    //异常状态
                    for (int j = 0; j < split.length; j++) {
                        Map<String, Object> map2 = new HashMap<>();
                        String s1 = split[j];
                        if (!"2".equals(s1) && !"6".equals(s1) && !"8".equals(s1) && !"12".equals(s1) && !"15".equals(s1)) {
                            a++;
                        }
                        String attend = getAttend(s1);
                        map2.put("attend", attend);
                        map2.put("dateTime", time);
                        map2.put("week", DateUtil.getWeekEnglish(time));
                        map2.put("zh_CN_week", DateUtil.getWeeks(time));
                        if (j < split1.length) {
                            if (!"".equals(split1[j]) && !"''".equals(split1[j])) {
                                String getdaytime = DateUtil.getdaytime(split1[j], "yyyy-MM-dd HH:mm:ss", "HH:mm");
                                map2.put("attendTime", getdaytime);
                            }
                        }
                        maps.add(map2);
                    }
                    if (a > 0) {
                        map.put("state", 1);
                    }
                    map.put("list", maps);
                }
            }
        } else {
            String[] split = shiftid.split(",");
            int weekInt = DateUtil.getWeekInt(dateTime);
            String s1 = split[weekInt - 1];
            logger.info("s1={}", s1);
            if ("0".equals(s1)) {
                for (int i = 0; i < userList.size(); i++) {
                    Map<String, Object> map = userList.get(i);
                    ArrayList<Map<String, Object>> maps = new ArrayList<>();
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("attend", "休息");
                    map2.put("dateTime", DateUtil.getdaytime(dateTime));
                    map2.put("week", DateUtil.getWeekEnglish(dateTime));
                    map2.put("zh_CN_week", DateUtil.getWeeks(dateTime));
                    map.put("state", 0);
                    maps.add(map2);
                    map.put("list", maps);
                }
            } else {
                String sql1 = "SELECT*FROM attendance_shift WHERE id=?";
                Map<String, Object> map1 = null;
                try {
                    map1 = attendJdbc.queryMap(sql1, s1);
                } catch (Exception e) {
                    logger.info("queryStudentAttend-->map1,error={}", e);
                }
                if (map1 != null) {
                    int type1 = (int) map1.get("type");
                    Map<String, Object> map4 = new HashMap<>();
                    if (type1 == 0) {//考勤类型:0 1天1次、1 1天2次
                        times[0] = map1.get("starttime").toString();
                        times[1] = map1.get("endtime1").toString();
                    } else {
                        times[0] = map1.get("starttime").toString();
                        times[1] = map1.get("endtime").toString();
                        times[2] = map1.get("starttime1").toString();
                        times[3] = map1.get("endtime1").toString();
                    }
                    for (int i = 0; i < userList.size(); i++) {
                        Map<String, Object> map = userList.get(i);
                        Object id = map.get("id");
                        String sql2 = "SELECT o.userid tid,o.attend_group_id,o.shift_id,o.attendtime,o.attendtype FROM attend o WHERE o.school_id=? AND o.userid=? " +
                                " AND o.attend_group_id=? AND o.state=0 AND TO_DAYS(o.attendtime)=TO_DAYS(?)  GROUP BY attendtime ORDER BY attendtime ";
                        List<Map<String, Object>> attendList = attendJdbc.queryAttendListMap(sql2, school_id, id, attend_groupid, dateTime);
                        Map<String, Object> map2 = attendList(attendList, map1, dateTime, id);
                        map.putAll(map2);
                    }
                }
            }
        }
        Map<String, Object> map = userList.get(0);
        List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");
        if (list != null && list.size() > 0) {
            for (int i1 = 0; i1 < list.size(); i1++) {
                Map<String, Object> map1 = list.get(i1);
                if (!"休息".equals(map1.get("attend").toString()) && !"缺勤".equals(map1.get("attend").toString())) {
                    map1.put("dktime", times[i1]);
                    map1.put("name", i1 % 2 == 0 ? "进班" : "离班");
                }
            }
        }
        return map;
    }
}
